How to read Array from plist iOS
First of all Check your plist looks like:
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:
Swift:
let dictionary = NSDictionary(contentsOfFile: Bundle.main.pathForResource("Values", ofType: "plist")!);
let array = dictionary?["arrayKey"] as! NSArray
print("dictionary=", dictionary, "\narray =", array)
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!
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.