Thursday, May 2, 2013

NSKeyedArchiver encodeObject example ios


encodeObject :forKey:

Encodes a given object and associates it with a given key.
- (void)encodeObject:(id)objv forKey:(NSString *)key
Parameters of [NSKeyedArchiver encodeObject]
objv
The value to encode. This value may be nil.
key
The key with which to associate objv. This value must not be nil.
Example of [NSKeyedArchiver encodeObject]
- (void) writeDataToArchive {
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                             initForWritingWithMutableData:data];

[archiver encodeObject:self.entriesArray forKey:@"entriesArray"];
[archiver finishEncoding];
BOOL result = [data writeToFile:[self dataFilePath]  atomically:YES];
[archiver release];
[data release];    
}
Example of [NSKeyedArchiver encodeObject]
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:YOURDICTIONARY forKey: YOURDATAKEY];
archiver finishEncoding];
[data writeToFile:YOURFILEPATH atomically:YES];
[data release];
[archiver release];