gpt4 book ai didi

ios - NSCFArray objectForKey 崩溃?

转载 作者:行者123 更新时间:2023-11-29 02:46:52 26 4
gpt4 key购买 nike

我正在尝试做一些相对简单的事情。我的 bundle 中有一个 .plist,我正在尝试将其加密保存到文档目录中。现在,在我尝试添加加密之前,它运行良好。然而,新的崩溃又出现了。

这是我保存 .plist 的方式:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Hi.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"GameSave" ofType:@"plist"];
NSData *plistData = [NSData dataWithContentsOfFile:bundle];
[NSKeyedArchiver archiveRootObject:plistData toFile:path];
plistData = [plistData AES256EncryptWithKey:@"536335"];
[plistData writeToFile:path atomically:YES];
}

这就是我检索 .plist 的方式(稍后更改值并重新保存)

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Hi.plist"];

//Get array and then current level dict
NSData *plistData = [[NSData alloc] initWithContentsOfFile:path];
plistData = [plistData AES256DecryptWithKey:@"1111"];

NSMutableArray *savegameArray = [[NSKeyedUnarchiver unarchiveObjectWithData:plistData] mutableCopy];
int objectIndex = [Singleton sharedInstance].levelNumber - 1;
NSMutableDictionary *levelDict = [[savegameArray objectAtIndex:objectIndex] mutableCopy];
[levelDict setObject:videoID forKey:@"RecordingURL"];
//Now put new dict back in array
[savegameArray replaceObjectAtIndex:objectIndex withObject:levelDict];

NSData *savedarrayData = [NSKeyedArchiver archivedDataWithRootObject:savegameArray];
savedarrayData = [savedarrayData AES256EncryptWithKey:@"1111"];
[savedarrayData writeToFile:path atomically:YES];

但是,每次我读到这一行时,在读取的代码中:NSMutableArray *savegameArray = [[NSKeyedUnarchiver unarchiveObjectWithData:plistData] mutableCopy]; 有一个 SIGABRT 崩溃打印:

'-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x16604140'

我做错了什么?

最佳答案

正如我们通过聊天发现的那样,我们必须首先将解密的 NSData 对象转换为正确的 NSPropertyListSerialization 序列化。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Hi.plist"];

NSData *plistData = [[NSData alloc] initWithContentsOfFile:path];
NSData *newData = [plistData AES256DecryptWithKey:@"1111"];

NSPropertyListFormat format;

NSMutableArray *savegameArray = [[NSPropertyListSerialization propertyListFromData:newData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:nil]mutableCopy];

NSLog(@"Array: %@",savegameArray);

关于ios - NSCFArray objectForKey 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007056/

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