XCode Cocoa/Objective-C Saving program data

Discussion in 'Mac Programming' started by ledd, Dec 14, 2007.

  1. ledd macrumors newbie

    Joined:
    Dec 26, 2006
    #1
    I am relatively new to Objective-C and Cocoa and have built an application in which I want to be able to save the current data. (AS in File - Save As - countDown.cd or somthing)
    After the save you could then Open the saved file and it would restore the app with the appropriate data etc...

    What is the best way of doing this?
    I know I can obviously make a text file with the stored data in key pairs and then parse on an open but is there a better/more standard approach.

    Thanks.
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    You probably want to look at NSArchiver, NSCoder, and related classes. This document has a lot more info on it.
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    If you're going to go down this route, use NSKeyedArchiver since it is better for backwards compatibility for future versions of your app.
     
  4. ledd thread starter macrumors newbie

    Joined:
    Dec 26, 2006
  5. ledd thread starter macrumors newbie

    Joined:
    Dec 26, 2006
    #5
    Ok so I checked it out and I thought I got it to work but I have a problem.

    Does anyone have an idea why this doesn't update my NSDatePicker when I load the saved file?
    Code:
    - (NSData *)dataRepresentationOfType:(NSString *)aType
    {
        NSDate* date = [datePicker dateValue];
        return [NSKeyedArchiver archivedDataWithRootObject: date];
    }
    
    - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
    {
        NSLog(@"About to read data of type %@", aType);
    	NSDate* date;
    	date = [NSKeyedUnarchiver unarchiveObjectWithData:data];	
    	if(date == nil){
    		NSLog(@"Test");
    		return NO;
    	}
    	else{
    		[datePicker setDateValue: date];
    		return YES;
    	}
    	return YES;
    }
    
    
    
     

Share This Page