gpt4 book ai didi

objective-c - NSKeyedArchiver archiveRootObject 总是返回 NO

转载 作者:太空狗 更新时间:2023-10-30 03:39:21 29 4
gpt4 key购买 nike

我是 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

最佳答案

当你想转换文件 NSURL 用于需要路径的方法时,你应该使用 path 方法,而不是 absoluteString方法。正如 path 方法文档所说:

If the URL object contains a file or file reference URL (as determined with isFileURL), the return value of this method [path] is suitable for input into methods of NSFileManager or NSPathUtilities. If the path has a trailing slash, it is stripped.

absoluteString 方法返回一个看起来像 file:///Users/... 的字符串,但您不需要那个 file:// 这些方法中的方案前缀正在寻找某些资源的路径。 path 方法返回您的文件路径,不受 file:// 前缀的影响。

关于objective-c - NSKeyedArchiver archiveRootObject 总是返回 NO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25300268/

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