NSCFArray objectForKey崩溃了吗?

时间:2014-07-29 01:48:48

标签: ios encryption nsarray sigabrt property-list

我正在尝试做一些相对简单的事情。我的捆绑包中有一个.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'

我做错了什么?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

希望这有用。

谢谢, 拉杰什..