Codementor Events

How to read Array from plist iOS

Published Mar 06, 2019

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

Discover and read more posts from Arpit Awasthi
get started
post commentsBe the first to share your opinion
Pat lafontaine
4 years ago

Hi,
I have a plist close from yours but inside the array, it s 25 dictionary! I m trying to get a bool value inside of those, lets say the second one. I follow your structure by adding:
NSDictionary *myDict = array[1];
BOOL myBool = [[myDict objectForKey:@“mainswitch”] boolValue];
But seem not to work! Look like value is always false but with theos I dont have any way to check…
I tryed with an alert but not able to compile and no way to read log…

What do you think?
Thanks!

Arpit Awasthi
4 years ago

Hi Pat,

Sorry for the late response. I just checked your comment. Next time please send me a DM as well.

So I see your code to get bool seems correct. The only thing that can get wrong here is the data type of stored boolean value. Share a screenshot of the dictionary so I can check.

Arpit.

Show more replies