gpt4 book ai didi

objective-c - iOS 12.0 替代使用已弃用的 archiveRootObject :toFile:

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:00 25 4
gpt4 key购买 nike

使用 iOS 12,archiveRootObject:toFile:已被弃用。谁能建议一种简化的替代方法来将对象归档到文件中?

//Generic example of archiver prior to iOS 12.0    
-(BOOL) archive:(id)archiveObject withFileName:(NSString*)filename
{
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
return [NSKeyedArchiver archiveRootObject:archiveObject toFile:path];
}

最佳答案

感谢@vadian 的提示,这是我在 iOS 12 下进行归档和取消归档的方法:

NSError *error = nil;

NSString *docsDir;
NSArray *dirPaths;

//Get the device's data directory:
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"appData.data"]];

//Archive using iOS 12 compliant coding:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@"foo" requiringSecureCoding:NO error:&error];
[data writeToFile:databasePath options:NSDataWritingAtomic error:&error];
NSLog(@"Write returned error: %@", [error localizedDescription]);

//Unarchive the data:
NSData *newData = [NSData dataWithContentsOfFile:databasePath];
NSString *fooString = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSString class] fromData:newData error:&error];

关于objective-c - iOS 12.0 替代使用已弃用的 archiveRootObject :toFile:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com