Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Loading/Saving Cocoa Objective C in NSDocument App

So I thought I was doing this the right way but apparently I am not because it isn't working. I am trying to change the NSDatePicker. I save the NSDate from the NSDatePicker when I save and then I load it and set it to the NSDatePicker when I load.
The file loads but the NSDatePicker doesn't change after I load it.
Here's my code...
Any thoughts?
/////////////////////////////////////////////////////////////
- (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;
}
}
/////////////////////////////////////////////////////////////

PBook 17", Mac OS X (10.4.6)

Posted on Dec 22, 2007 5:32 PM

Reply
6 replies

Dec 22, 2007 7:11 PM in response to nsutt

Okay if your code doesn't work it's simply because you don't use the good methods... API change from time to time, so now, instead of those two method you need to use :

To load :
\- (BOOL)readFromData: (NSData *)data ofType: (NSString *)typeName error: (NSError **)outError

and :

To save :
\- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError

The two methods you use are deprecated since Tiger.

Dec 22, 2007 11:20 PM in response to PsychoH13

I am aware the fact that Leopard has adopted the newer functions but the older ones should and do still work and it is not "simply because I don't use the good methods." These "deprecated" methods work just fine. Lets not forget Tiger was only surpassed a couple of months ago.

I think my problem might lie in the fact that these function calls happen before the creation of the NSDatePicker object. Thus calling a function from an uninitialized reference to NSDatePicker would of course result in no change, since it doesn't yet exist.

Dec 23, 2007 4:14 AM in response to nsutt

Indeed, the NSDatePicker doesn't exist yet when the load method is called... So to solve that problem, you need to hold the date value somewhere... So here ou just have to add an NSDate instance variable to your document class, it's generally what is done, you keep your document values in your document instance variables, and then after loading, you put the value into the NSDatePicker inside the -(void)awakeFromNib method :


- (void)awakeFromNib
{
if(myDate != nil)
[datePicker setDateValue:myDate];
}

Loading/Saving Cocoa Objective C in NSDocument App

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.