How to read Array from plist iOS
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.
NSDictionary * values=[[NSDictionary alloc] initWithContentsOfFile:@"values.plist"]; NSArray *arrayValues=[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]];
5 Solutions Collect From Internet About “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
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Values" ofType:@"plist"]]; NSLog(@"dictionary = %@", dictionary); NSArray *array = [dictionary objectForKey:@"keyarray1"]; NSLog(@"array = %@", array);
Here is the complete screen shot (with log output) of my work window:
let dictionary = NSDictionary(contentsOfFile: Bundle.main.pathForResource("Values", ofType: "plist")!); let array = dictionary?["arrayKey"] as! NSArray print("dictionary=", dictionary, "\narray =", array)
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”).
then the xml looks like this:
On my view controller I initialize directly the Array instead of initializing the Dictionary then get object for key “Root”:
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
After you have the path down correctly, you can then get the array from the dictionary without any problems, with something like
Final note: there is a difference between
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.
Can you try following code?
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"values" ofType:@"plist"]; NSDictionary * values=[[NSDictionary alloc] initWithContentsOfFile:plistPath]; NSArray *arrayValues=[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]]; NSLog(@"arrayValues = %@",arrayValues);
I got following output in log:-
arrayValues = ( keyitem1, keyitem2 )
NSString *path = [[NSBundle mainBundle] bundlePath]; NSString *finalPath = [path stringByAppendingPathComponent:@"values.plist"]; NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithContentsOfFile:finalPath]; NSArray *arr =[dict valueForKey:@"keyarray1"]; NSLog(@"%@",arr);
- Make hide/unhide master view controller work in landscape the same as portrait
- Checking the size of an object in Objective-C
- How big should a UIBarButtonItem image be?
- How to take screenshot of portion of UIView?
- How to remove all gesture recognizers from a UIView in Swift
- Xcode 5 Cocoa App Fails to Load Nib on OS X 10.6
- How to verify ios In-App Purchase on your server
- Media Query not working for iPhone and iPad
- Xcode 4 and Allegro 5.1 – Linker can't find Framework
- What is the recommended method of styling an iOS app?
- Realm's Swift module compiled with Swift 3.0 cannot be imported in Swift 3.0.1
- iPhone – Track cellular data usage
- React Native run-ios build fail after xcode update to 8.0.
- memory leakage in system libraries
- dismiss keyboard with a uiTextView