How to read Array from plist iOS

First of all Check your plist looks like:

enter image description here

Now write following lines where you are accessing your plist

Objective-C:

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Values" ofType:@"plist"]];
NSArray *array = [dictionary objectForKey:@"keyarray1"];
NSLog(@"dictionary = %@ \narray = %@", dictionary, array);

Here is the complete screen shot (with log output) of my work window:

enter image description here

Swift:

let dictionary = NSDictionary(contentsOfFile: Bundle.main.pathForResource("Values", ofType: "plist")!);
let array = dictionary?["arrayKey"] as! NSArray
print("dictionary=",  dictionary, "\narray =",  array)

enter image description here


I know this question is too old and I'll just like to add more information for those people encounter this recently.

In my case I created a plist as Array(plist default is Dictionary with key "Root").

enter image description here

then the xml looks like this:

enter image description here

On my view controller I initialize directly the Array instead of initializing the Dictionary then get object for key "Root":

enter image description here

Note: I only wanted to add this info since I only see initializing the Dictionary then get object for keys. Hope it will help you guys.


Where is the plist stored? Is it in the app bundle? If so, you probably want to use [[NSBundle mainBundle] pathForResource:@"values" ofType:@"plist"] to get the path, instead of hardcoding @"values.plist".

After you have the path down correctly, you can then get the array from the dictionary without any problems, with something like [values objectForKey:@"keyarray"].

Final note: there is a difference between objectForKey: and valueForKey:. You're currently using valueForkey: which is part of NSKeyValueCoding, a protocol that NSDictionary happens to conform to. You should use objectForKey: (or syntactic sugar accessors, i.e. dictionary[@"key"]) instead, as they are the proper ways of accessing a value from a dictionary.