Restoring NSArray from file

I have an array that I serialize, encrypt and then write into a file. The data itself appears to be good but I'm having problems restoring the array back from the file. Here is what I do

    NSString *filename = [[self getTransactionLogPath] stringByAppendingPathComponent:transactionLogName];
    NSData *data = [[NSData alloc] initWithContentsOfFile:filename];
    NSLog(@"encrypted data: %@", data);
    EncryptedData *decoder = [[EncryptedData alloc] init];
    NSData *decrypted = [decoder reverseTransformedValue:data];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:decrypted];

I get the filename and read the data from file. EncryptedData decoder runs AES128 on the data and finally I unarchive the array.

The problem is that unarchiveObjectWithData throws an exception

[NSKeyedUnarchiver initForReadingWithData:]:

incomprehensible archive

The tricky thing is that the code works fine in simulator if I keep the NSLog line ie I print out the data after reading the file.

On device the NSLog() does not help. Is this a threading problem where the unarchiver starts before the data is read?

I tried adding a delay instead of the NSLog() line but that didn't help.

Any other ways to do the job if I want to encrypt the array before writing to file?

链接地址: http://www.djcxy.com/p/68340.html

上一篇: 在目标c中创建JSON,无效的JSON

下一篇: 从文件恢复NSArray