PDA

View Full Version : loading and access plist data




StefanDevil
Aug 15, 2010, 11:37 AM
Hi,

i have a plist in the following form:

Root (array)--->

item 1 (dictionary)
----> Sch (string)
---> Name (string)
----> price (number)

item 2 (dictionary)----> .....same as item 1


how can i access each row (item1 to ...) and the its child (Sch, Name etc.)? one at a time?

i use:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
to load the file. how such i go about accessing each child?

what i am trying to do is, i have a NSString *messeage, what i want to do is to search the whole plist for matching string and display the whole item 1. Any suggestion?

Thanks for the help! gd day!



nebo1ss
Aug 15, 2010, 11:44 AM
Not sure what computer you have but on a MAC I use PListedit pro

StefanDevil
Aug 15, 2010, 11:56 AM
Not sure what computer you have but on a MAC I use PListedit pro

i mean how can i access, as in load the plist, in xcode and retrieve each data.

PhoneyDeveloper
Aug 15, 2010, 04:04 PM
You show the plist as being an array of dictionaries but the code you show loads the plist as a dictionary. Assuming it's really an array then load the plist into an array. Then you iterate the array just like any other array to access its members.

StefanDevil
Aug 16, 2010, 12:35 PM
thks for the help, i manage to solve this problem.
now i have another problem.
i have a new plist in this form:


<dict>
<key>11231654</key>
<array>
<string>hello</string>
<string>goodbye</string>
</array>
<key>78978976</key>
<array>
<string>ok</string>
<string>cancel</string>
</array>


i load the plist using:


NSString *path = [[NSBundle mainBundle] pathForResource:
@"data" ofType:@"plist"];
// Build the array from the plist
NSMutableArray *array3 = [[NSMutableArray alloc] initWithContentsOfFile:path];


what i want to do is, i have a "label.text" which is some numbers,
i would like to go to the match key (11231654 for example) and search its child.

so firstly, i must compare label.text with all the key available.

i 1st try to load one key and display it(just to make sure i am accessing the data correctly)

i use:



NSString *myString = [array3 objectAtIndex:0];
label.text = myString;


this will make label.text =11231654 ?

but its not displaying?

chown33
Aug 16, 2010, 07:02 PM
You've made the same error as your original post, but in reverse.

Your original code was wrong because you were loading an <array> plist into an NSDictionary.
Your latest code is wrong because you're loading a <dict> plist into an NSMutableArray.

These data structures are not interchangeable.

You must load a <dict> plist file into an NSDictionary (or its subclass NSMutableDictionary). Similarly, an <array> plist file must be loaded into an NSArray (or its subclass NSMutableArray).

StefanDevil
Aug 17, 2010, 12:31 AM
i see, i have change it. now how can i get the key value, 11231654?

as in i would to have a NSString = *number;
number will be equal to 11231654, so that i can use number to compare will others.

thks for the help!

chown33
Aug 17, 2010, 12:59 AM
i see, i have change it. now how can i get the key value, 11231654?

as in i would to have a NSString = *number;
number will be equal to 11231654, so that i can use number to compare will others.

thks for the help!

Think it through. There's nothing difficult here.

How do you usually retrieve the object for a key from an NSDictionary?
With objectForKey:.

Since plist keys are always strings (Ref. 1),you can use any expression that results in a numeric NSString object. Examples:
@"11231654"

[NSString stringWithFormat:@"%i", 11231654]

[[NSNumber numberWithInteger:11231654] stringValue]

someLabelContainingNumbers.text



(Ref. 1)
http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man5/plist.5.html
When using CFDictionary as a property list, all keys must be strings.

StefanDevil
Aug 17, 2010, 12:24 PM
hi, thks for the reply.

if i use:

array3 objectForKey:@"11231654"


i will be "looking" at the array inside the key 11231654, correct?

how can i "look at" the key(s) inside array3?

correct me if, i am wrong, but i should not be using objectforkey here.

grimjim
Aug 18, 2010, 03:07 AM
hi, thks for the reply.

if i use:

array3 objectForKey:@"11231654"


i will be "looking" at the array inside the key 11231654, correct?

how can i "look at" the key(s) inside array3?

correct me if, i am wrong, but i should not be using objectforkey here.

What "key(s) inside array3"? It's an array. It doesn't have keys. What you have done is to return the NSArray object whose key is "11231654" that is stored in the plist. If you want to get at the contents of the array, you do so in the same way as any other NSArray. If you look at the contents of the plist that you posted, you will see that the key "11231654" contains two string objects. No keys are specified for them.

So, yes, you should use objectForKey: to return the NSArray object, and then fast enumeration or a simple for loop to iterate through it and work on the objects it contains. Of course, you will need to be sure that array3 is of the correct type (i.e. that it is an NSArray, not an NSDictionary).