Save Mechanics I have used:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:self forKey:@"Save"];
[archiver finishEncoding];
[data writeToFile:filePath atomically:YES];
[archiver release];
[data release];
I have used
[[NSFileManager defaultManager] fileExistsAtPath:savePath];
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:[SynchronizedData createPath] error:&error]
to check the existences of my file. They confirm the file exist.
However I cannot load the file with the same directory path
//NSData *encodedData = [[NSFileManager defaultManager] contentsAtPath:[SynchronizedData createPath]];
NSData *encodedData = [[[NSData alloc] initWithContentsOfFile:tempFilePath] autorelease];
if (encodedData == nil) {
NSLog(@"Save cannot open while loading!");
return nil;
}
The above encodedData, both returned nil. (autorelease is not the problem, i’ve checked)
Any ideas what’s wrong?
I found similar case in previous thread, I tried retain once more my tempFilePath, it doesn’t matter, the problem still exist.
I have done the save in a more simpler mechanism than the above one.
The problem remained unsolved for getting the NSData from directory. Maybe due to Read Permission mentioned by @paxdiablo.
Thus I use “Documents” folder instead of “Library”. The NSData open has no error.
A file can exist but still not be readable. You should probably start by checking the permissions on that file.
Also, use the
initWithContentsOfFile:options:error:variant to get an error back – that should hopefully tell you if there’s a problem.And, based on your comment that you’re seeing:
it appears the permissions are the problem. If you look at the Foundation constants, you’ll see:
meaning that you have no permission to read that file.