NSKeyedArchiver archiveRootObject 总是返回 NO
Posted
技术标签:
【中文标题】NSKeyedArchiver archiveRootObject 总是返回 NO【英文标题】:NSKeyedArchiver archiveRootObject always returns NO 【发布时间】:2014-10-07 15:29:35 【问题描述】:我是 Objective C 的新手,正在编写一些简单的程序来熟悉自己。
尝试使用 NSKeyedArchiver 保存/读取信息,但每次我都无法创建 .plist 文件。我环顾四周,无法找到答案。谁能帮我理解为什么“出了问题……”总是最终输出?
谢谢!
// main.m
#import <Foundation/Foundation.h>
#import "Domicile.h"
void CreateAndArchive(NSMutableArray* objs, NSURL *saveLocation)
NSLog(@"Creating DOMICILE objects...");
Domicile *now = [[Domicile alloc] init];
Domicile *then = [[Domicile alloc] init];
now.HouseName = @"place1;
now.Address = @"506 99th street";
now.Residents = [[NSMutableArray alloc] initWithObjects: @"Erik", @"Jeremiah", @"Alex", @"Ryan", @"Kyle", @"Connor", nil];
now.City = @"aCity";
now.State = @"CA";
now.Zip = @"12345";
then.HouseName = @"place2";
then.Address = @"1011 E Fairmont street";
then.Residents = [[NSMutableArray alloc] initWithObjects: @"Erik", @"Asa", @"Ryan", @"Gabe", @"Josh", nil];
then.City = @"anotherCity";
then.State = @"VA";
then.Zip = @"54321";
NSMutableArray *saveThis = [[NSMutableArray alloc] initWithObjects: now, then, nil];
NSLog(@"saving to %@", [saveLocation absoluteString]);
if ([NSKeyedArchiver archiveRootObject:saveThis toFile:[saveLocation absoluteString]])
NSLog(@"SAVED!");
else
NSLog(@"Something went wrong...");
NSMutableArray* Load(NSURL *location)
NSLog(@"Loading data from %@", [location absoluteString]);
return [NSKeyedUnarchiver unarchiveObjectWithFile: [location absoluteString]];
int main(int argc, const char * argv[])
@autoreleasepool
NSURL *dir = [[NSFileManager defaultManager] URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *file = [dir URLByAppendingPathComponent:@"data.plist"];
NSMutableArray *domiciles = [[NSMutableArray alloc] init];
if ([[NSFileManager defaultManager] fileExistsAtPath:[file absoluteString]])
domiciles = Load(file);
NSLog(@"data loaded!");
NSLog(@"displaying for your convenience:");
for (Domicile *dom in domiciles)
NSLog(@"\n\n%@", dom);
else
CreateAndArchive(domiciles, file);
return 0;
// Domicile.m
#import "Domicile.h"
@implementation Domicile
-(instancetype)initWithCoder:(NSCoder *)coder
self = [super init];
if (self)
_HouseName = [coder decodeObjectForKey:@"name"];
_Residents = [coder decodeObjectForKey:@"residents"];
_Address = [coder decodeObjectForKey:@"address"];
_City = [coder decodeObjectForKey:@"city"];
_State = [coder decodeObjectForKey:@"state"];
_Zip = [coder decodeObjectForKey:@"zip"];
return self;
-(void) encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:self.HouseName forKey:@"name"];
[aCoder encodeObject:self.Residents forKey:@"residents"];
[aCoder encodeObject:self.Address forKey:@"address"];
[aCoder encodeObject:self.City forKey:@"city"];
[aCoder encodeObject:self.State forKey:@"state"];
[aCoder encodeObject:self.Zip forKey:@"zip"];
-(NSString *) description
NSString *desc = [[NSString alloc] initWithFormat: @"House : %@\n%@\n%@, %@\n%@\n", self.HouseName, self.Address, self.City, self.State, self.Zip];
return desc;
@end
【问题讨论】:
查看***.com/questions/14326533/… 【参考方案1】:当您想要转换文件NSURL
以用于需要路径的方法时,您应该使用path
方法,而不是absoluteString
方法。正如path
方法文档所说:
如果 URL 对象包含文件或文件引用 URL(由
isFileURL
确定),则此方法的返回值 [path
] 适合输入到NSFileManager
或NSPathUtilities
的方法中。如果路径有尾部斜杠,则将其剥离。
absoluteString
方法返回一个看起来像 file:///Users/...
的字符串,但您不希望在这些正在寻找某些资源的路径的方法中使用 file://
方案前缀。 path
方法返回您的文件路径,不受 file://
前缀的影响。
【讨论】:
以上是关于NSKeyedArchiver archiveRootObject 总是返回 NO的主要内容,如果未能解决你的问题,请参考以下文章
从 NSKeyedArchiver 中提取 NSString
如何将 UIDocumentPickerViewController 导入限制为 .archive 文件? (NSKeyedArchiver)