Re: reading a plist
Re: reading a plist
- Subject: Re: reading a plist
- From: Chris Hanson <email@hidden>
- Date: Sun, 21 Jul 2002 03:52:48 -0500
At 10:10 PM -0400 7/20/02, Koen van der Drift wrote:
If I use the code example from Hillegass' book:
path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
myList = [NSDictionary dictionaryWithContentsOfFile: path];
myArray = [myList objectForKey:@"testkeys"];
NSLog(@"test = %@", myArray);
Then I get all keys and values. If I use myArray = [myList
objectForKey:@"KeyA2"] then myList is null.
You have nested dictionaries there. The file contains one
dictionary, which you've fetched and assigned to myList. In this
dictionary, there's a key called "testkeys". This key has a
dictionary as its value. *That* dictionary has a key "KeyA" which
has *another* dictionary as its value.
So to get the value of KeyA in your file, you'd say
myList = [NSDictionary dictionaryWithContentsOfFile:path];
testDictionary = [myList objectForKey:@"testkeys"];
keyADictionary = [testDictionary objectForKey:@"KeyA"];
So, how do I store in an array all the values from a similar key, for
instance KeyA3, KeyB3, etc?
The way it looks like your file is structured, you'd have to iterate
over the keys of the testkeys dictionary, get the dictionary
corresponding each key, and then get the similar values from each
dictionary and add them to an array.
Fundamentally, how you really do it is to understand (1) how
dictionaries, arrays, and enumeration work in Cocoa and (2) how the
data in your file is actually structured.
And is it possible to get an array with KeyA,
KeyB, ... (thus not the values)?
Yes. You can ask any dictionary for an array of keys by sending it
-allKeys. You can also ask any dictionary to create a key enumerator
if you're going to be iterating over all keys in it.
-- Chris
--
Chris Hanson | Email: email@hidden
bDistributed.com, Inc. | Phone: +1-847-372-3955
Making Business Distributed | Fax: +1-847-589-3738
http://bdistributed.com/ | Personal Email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.