jovhenni19
jovhenni19

Reputation: 450

NSKeyedUnarchiver decodeObjectForKey returning null class

I have an archive which is stored in "Presidents.plist" file. It exist and I can locate it using pathForResource method. And also I do get the data using initWithContentsOfFile method of NSData to retrieve the data. The problem now is in this lines of code.

NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents"
                                                 ofType:@"plist"];
NSData *data; 
NSKeyedUnarchiver *unarchiver;
data = [[NSData alloc] initWithContentsOfFile:path];
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id presidents = [unarchiver decodeObjectForKey:@"Presidents"];

For some reason it is returning a null class instead of my expected NSArray class. Can anyone tell me the reason this is happening and how can I solve this dilemma?

I'm getting the class like this...

NSLog(@"presidents class = %@", NSStringFromClass([presidents class]));

Upvotes: 0

Views: 2013

Answers (3)

ChikabuZ
ChikabuZ

Reputation: 10185

I had the same problem, Method 2 helped me.

Change the "BIDPresident" to the "President" in Presidents.plist using TextWrangler or try my file http://www.fileswap.com/dl/o29Sxm48g0/Presidents.plist.html

Upvotes: 1

RGuy8032
RGuy8032

Reputation: 71

In more recent versions of iOS the root object of a pList must not be an array. I had this problem recently. Try using a dictionary instead.

Upvotes: 0

Sree
Sree

Reputation: 907

You can access the object by:

    NSMutableDictionary dict = [NSKeyedUnarchiver unarchiveObjectWithFile:archieverPath];

archieverPath is the path of the plist. and you can return the object by

return [dict objectForKey:key];

Upvotes: 0

Related Questions