All I have to do is archive an object, but the method archiveRootObject continues to return NO
This is my code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString * cacheDirectory = [paths objectAtIndex:0];
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MyAppCache"];
NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:@"archive.data"];
< br />if(![[NSFileManager defaultManager] fileExistsAtPath:fullPath]){
[[NSFileManager defaultManager] createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
}
< br />NSArray *array = [NSArray arrayWithObjects:@"hello", @"world", nil];
NSLog(@"Full Path: %@", fullPath);
BOOL res = [ NSKeyedArchiver archiveRootObject:array toFile:fullPath];
if(res){
NSLog(@"YES");
}else{
NSLog(@"NO ");
}
Every time I run it, it prints NO.
Any help Help, would be greatly appreciated!
I spent a few hours trying to solve this problem, but I have not finished thinking about it.
< /p>
What I want to do is archive an object, but the method archiveRootObject continues to return NO
This is my code:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MyAppCache"];
NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:@"archive.data"];
if(![[NSFileManager defaultManager] fileExistsAtPath:fullPath]){
[[NSFileManager defaultManager] createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes :nil error:nil];
}
NSArray *array = [NSArray arrayWithObjects:@"hello", @"world", nil];
NSLog(@"Full Path: %@", fullPath);
BOOL res = [NSKeyedArchiver archiveRootObject:array toFile:fullPa th];
if(res){
NSLog(@"YES");
}else{
NSLog(@"NO");
}
Every time I run it, it prints NO.
Any help would be greatly appreciated!
You create a directory with the path fullPath, and then try to write the file on the same path. It is impossible to overwrite the directory with such a file. Use cacheDirectory String creation directory.