How to read Array from plist iOS
ios, objective-c, swift
Solution
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)
Problem
I am trying to read plist which contains array ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>key1</key> <string>value1</string> <key>key2</key> <string>value2</string> <key>keyarray1</key> <array> <string>keyitem1</string> <string>keyitem2</string> </array> </dict> </plist> ``` when i try to read valueForKey:@"keyarray1", I get null value. I tried to read as a string and array nut no use. My Code ``` NSDictionary * values=[[NSDictionary alloc] initWithContentsOfFile:@"values.plist"]; NSArray *arrayValues=[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]]; ```