Unarchiving problem using NSKeyedUnarchiver

Hello Experts,

I am saving my application's state using NSKeyedArchiver.

NSMutableData *data;
NSKeyedArchiver *archiver;

data = [NSMutableData data];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

[archiver encodeObject:MyObjectToSave forKey:@"SavedStateKey"];
[archiver finishEncoding];
[data writeToFile:@"/tmp/SavedState" atomically:YES];
NSLog(@"Encoding Done");
[archiver release];



And Decoding the savedObject by the following code:


NSData *data;
NSKeyedUnarchiver *unarchiver;

data = [NSData dataWithContentsOfFile:@"/tmp/SavedState"];
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
MyObjectToSave = [[unarchiver decodeObjectForKey:@"SavedStateKey"] retain];
[unarchiver finishDecoding];
[unarchiver release]



The Coding is working fine on the simulator.
When running on the real device, It saves the state successfully but at the decoding time, it gives the following error:

* -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL


i have tried changing the location to following with no luck.

[data writeToFile:@"SavedState" atomically:YES];


Is there any specific folder in the device, where i need to save the archeived data.


Thanks in advance.

Macbook, Mac OS X (10.5.5)

Posted on Jan 4, 2009 11:41 PM

Reply
3 replies

Jan 5, 2009 12:19 AM in response to Rajiv Verma

Further Info:

On real device the Data is not even archeived. I tested it using following code:

BOOL result;
result = [data writeToFile:@"/tmp/SavedState" atomically:YES];
if(result){
NSLog(@"Encoding Done ");
}
else{
NSLog(@"Encoding Cannot be Done ");
}



Every time it prints "Encoding cannot be done".

On Simulator it is working fine.

Message was edited by: Rajiv Verma

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Unarchiving problem using NSKeyedUnarchiver

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