標籤:
歸檔:
NSMutableData *data = [[NSMutableData alloc] init];
//建立歸檔輔助類
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//編碼
[archiver encodeObject:@"2009-12-09" forKey:@"dataTime"];
//結束編碼
[archiver finishEncoding];
//寫入
[data writeToFile:[self getFilePathWithModelKey:[NSString stringWithFormat:@"test"]] atomically:YES];
方法://得到Document目錄
-(NSString *) getFilePathWithModelKey:(NSString *)modelkey
{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[array objectAtIndex:0] stringByAppendingPathComponent:modelkey];
}
解檔:
NSData * data = [[NSData alloc] initWithContentsOfFile:[self getFilePathWithModelKey:[NSString stringWithFormat:@"test"]]];
//解檔輔助類
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSString * time = [unarchiver decodeObjectForKey:@"dataTime"];
//關閉解檔
[unarchiver finishDecoding];
iOS 歸檔解檔