Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ledd

macrumors newbie
Original poster
Dec 26, 2006
23
0
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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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.
You probably want to look at NSArchiver, NSCoder, and related classes. This document has a lot more info on it.
 

ledd

macrumors newbie
Original poster
Dec 26, 2006
23
0
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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.