0
votes

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

3 Answers

1
votes

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

0
votes

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

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.