View Full Version : XCode Cocoa/Objective-C Saving program data
ledd
Dec 14, 2007, 06:45 PM
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
Dec 14, 2007, 07:04 PM
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 (http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/Archiving.html) has a lot more info on it.
kainjow
Dec 15, 2007, 12:53 AM
You probably want to look at NSArchiver, NSCoder, and related classes. This document (http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/Archiving.html) has a lot more info on it.
If you're going to go down this route, use NSKeyedArchiver since it is better for backwards compatibility for future versions of your app.
ledd
Dec 15, 2007, 08:14 PM
Thanks a bunch. I will check it out.
ledd
Dec 22, 2007, 03:14 PM
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?
- (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;
}
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.