archiveRootObject :toFile:
Archives an object graph rooted at a given object by encoding it into a data object then atomically writes the resulting data object to a file at a given path, and returns a Boolean value that indicates whether the operation was successful.
Parameters
- rootObject
- The root of the object graph to archive.
- path
- The path of the file in which to write the archive.
Return Value of [NSKeyedArchiver archiveRootObject]
YES
if the operation was successful, otherwise NO
.Discussion
The format of the archive is
NSPropertyListBinaryFormat_v1_0
.
Example of [NSKeyedArchiver archiveRootObject]
NSString *localPath = @"Documents/my.archive";
NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:localPath];
[NSKeyedArchiver archiveRootObject:archive toFile:fullPath];
Example of [NSKeyedArchiver archiveRootObject]
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString* docFile = [docDir stringByAppendingPathComponent: @"Somefile.plist"];
NSError* error;
if([NSKeyedArchiver archiveRootObject:tempData toFile:docFile]) {
NSDictionary *fileAttributes = [NSDictionary
dictionaryWithObject:NSFileProtectionComplete
forKey:NSFileProtectionKey];
if([[NSFileManager defaultManager] setAttributes:fileAttributes
ofItemAtPath:docFile error: &error]) {
NSLog(@"Success");
}
else {
NSLog@(%@", error);
}
}