Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

My Code

NSDictionary * values=[[NSDictionary alloc] initWithContentsOfFile:@"values.plist"];
NSArray *arrayValues=[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]];
like image 830
ram2013 Avatar asked Apr 28 '13 23:04

ram2013


4 Answers

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

like image 82
rptwsthi Avatar answered Oct 17 '22 18:10

rptwsthi


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.

like image 23
Arnlee Vizcayno Avatar answered Oct 17 '22 17:10

Arnlee Vizcayno


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.

like image 3
zadr Avatar answered Oct 17 '22 17:10

zadr


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
)
like image 3
Sunil Zalavadiya Avatar answered Oct 17 '22 16:10

Sunil Zalavadiya