Bronze Challenge, another approach


#1

Hi
I used the methods in the app delegate instead to save the lines

#import "BNRAppDelegate.h"
#import "BNRDrawViewController.h"

@interface BNRAppDelegate()

@property BNRDrawViewController *dvc;
-(void)saveLinesInView;

@end

@implementation BNRAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    _dvc = [[BNRDrawViewController alloc]init];
    self.window.rootViewController = _dvc;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self saveLinesInView];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [self saveLinesInView];
}
//Saving the array to the disk
- (void)saveLinesInView
{
    BNRDrawView *bv = _dvc.bdv;
    [NSKeyedArchiver archiveRootObject:bv.finishedLines toFile:_dvc.linesArchivePath];
    NSLog(@"Did archive %lu", (unsigned long)bv.finishedLines.count);
    
}

And to load them I used the "viewWillAppear delegate:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    NSMutableArray *finishedLines = [NSKeyedUnarchiver unarchiveObjectWithFile:[self linesArchivePath]];
    if( finishedLines )
    {
        _bdv.finishedLines = finishedLines;
    }
    NSLog(@"Did load archives");
    
}

And I did the same to the variables as the other guy

implementation BNRLine

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeCGPoint:self.begin forKey:@"beginPoint"];
    [aCoder encodeCGPoint:self.end forKey:@"endPoint"];
    NSLog(@"encoding begin.x %f & begin.y %f", self.begin.x, self.begin.y);
    NSLog(@"encoding end.x %f & end.y %f", self.end.x, self.end.y);
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if(self)
    {
        self.begin = [aDecoder decodeCGPointForKey:@"beginPoint"];
        self.end = [aDecoder decodeCGPointForKey:@"endPoint"];
        NSLog(@"decoding begin.x %f & begin.y %f", self.begin.x, self.begin.y);
        NSLog(@"decoding end.x %f & end.y %f", self.end.x, self.end.y);
    }
    return self;
}


@end