我是目标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
答案 0 :(得分:22)
如果要转换文件NSURL
以便在需要路径的方法中使用,则应使用path
方法,而不是absoluteString
方法。正如path
方法文档所说:
如果URL对象包含文件或文件引用URL(由
isFileURL
确定),则此方法[path
]的返回值适合输入NSFileManager
的方法或NSPathUtilities
。如果路径有一个尾部斜杠,则会将其删除。
absoluteString
方法返回的字符串看起来像file:///Users/...
,但您不希望这些方法中的file://
方案前缀为某些方法寻找路径资源。 path
方法返回您的文件路径,不受file://
前缀的影响。