Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass nil to archivedDataWithRootObject return weird NSData

Somewhere in my code

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:someArray];

I expect data to be nil if someArray is nil, but it return some data I don't understand.

I print it out as below <62706c69 73743030 d4010203 0405080a 0b542474 6f705824 6f626a65 63747358 24766572 73696f6e 59246172 63686976 6572d106 0754726f 6f748000 a1095524 6e756c6c 12000186 a05f100f 4e534b65 79656441 72636869 76657208 11161f28 32353a3c 3e444900 00000000 00010100 00000000 00000c00 00000000 00000000 00000000 00005b>

I tried NSData *data = [NSKeyedArchiver archivedDataWithRootObject:nil]; to make sure the argument is certainly nil.

I read the documentation and cannot find any explanation about this.

So what exactly is happening here?

Edit I write data to file, and it turn out to be a plist like this. It may be the way the NSKeyedArchiver store objects.

nil plist file

like image 647
Gary Lyn Avatar asked Apr 10 '14 14:04

Gary Lyn


Video Answer


1 Answers

That data looks like an 'empty' plist. You can convert it to a string and log it to check yourself. Because the archiver uses a set format you will always get a wrapper of data back.

Technically it could be coded so that that wasn't necessary, but you have no control over it.

If you need to not have an archive for a nil or empty source array then your code should check and not execute the archive (and delete any existing archive file if required).

like image 143
Wain Avatar answered Nov 15 '22 08:11

Wain