Question

I am trying to do something relatively simple. I have a .plist in my bundle and I am trying to save it to the documents directory with encryption. Now before I tried added encryption, it worked fine. However a new crash has arose.

This is how I save my .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];
}

Then this is how I retrieve my .plist (and later change a value and re-save)

        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];

However, in the read code every time I get to this line: NSMutableArray *savegameArray = [[NSKeyedUnarchiver unarchiveObjectWithData:plistData] mutableCopy]; There is a SIGABRT crash which prints:

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

What am I doing wrong?

Was it helpful?

Solution

As we discovered through our chat, we had to first convert the decrypted NSData object to a proper NSPropertyListSerialization serialization.

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);

OTHER TIPS

You just need to allocate memory to your NSMutableArray like... NSMutableArray *arrayName = [[NSMutableArray alloc] init]; Until you do that crash will arose..

Hope this is helpful.

Thanks, Rajesh..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top