0

我正在尝试做一些相对简单的事情。我的包中有一个 .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:]: 无法识别的选择器发送到实例 0x16604140'

我究竟做错了什么?

4

2 回答 2

1

正如我们在聊天中发现的那样,我们必须首先将解密的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);
于 2014-07-29T04:11:45.973 回答
0

您只需要为您的 NSMutableArray 分配内存,例如... NSMutableArray *arrayName = [[NSMutableArray alloc] init]; 直到你这样做,崩溃才会出现..

希望这会有所帮助。

谢谢,拉杰什..

于 2014-07-29T11:33:10.717 回答