gpt4 book ai didi

cocoa - 归档 NSString 字典

转载 作者:行者123 更新时间:2023-12-03 17:31:54 26 4
gpt4 key购买 nike

我想存档只包含 NSString 的字典,但是当我取消存档时,发生错误:

[NSKeyedUnarchiver initForReadingWithData:]: non-keyed archive cannot be decoded by NSKeyedUnarchiver

我的代码是:

- (IBAction)startArchive:(id)sender {
NSString *path = [self pathForDataFile];
NSMutableDictionary *ageDictionary= [NSMutableDictionary dictionary];
[ageDictionary setValue:@"12" forKey:@"Jack"];
[ageDictionary setValue:@"22" forKey:@"John"];
[NSArchiver archiveRootObject:ageDictionary toFile:path];
}


- (IBAction)unarchive:(id)sender {
NSString *path = [self pathForDataFile];
NSDictionary *rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@",rootObject);
}

最佳答案

原因是 Unarchiver。您使用 NSArchiver 进行归档。作为解档程序,您应该使用 NSUnarchiver。如果您使用 NSKeyedArchiver,则应该使用 NSKeyedUnarchiver。

你把他们搞糊涂了。

流动代码是测试工作:

    #import <Foundation/Foundation.h>

void startArchive() {
NSString *path = @"/Users/dinosaur/Desktop/test.plist";
NSMutableDictionary *ageDictionary= [NSMutableDictionary dictionary];
[ageDictionary setValue:@"12" forKey:@"Jack"];
[ageDictionary setValue:@"22" forKey:@"John"];
[NSArchiver archiveRootObject:ageDictionary toFile:path];
}


void unarchive() {
NSString *path = @"/Users/dinosaur/Desktop/test.plist";
NSDictionary *rootObject = [NSUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@",rootObject);
}


int main(int argc, char *argv[]) {
@autoreleasepool {
startArchive();
unarchive();
}
}

============

这是日志:

2012-08-27 14:03:11.511 无标题[7930:707] { jack =12; 约翰 = 22;}

关于cocoa - 归档 NSString 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136700/

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