开发者

iOS: problem when populating Picker from plist file

I've been following the tutorial at http://iphonebyradix.blogspot.com/2011/03/read-and-write-data-from-plist-file.html showing how to extract data from a plist into an array so I can put it in a picker.

I had the picker populating fine from an Array I created manually, but I'm trying to read an array from the plist file using the same method as in that tutorial but I'm getting no data.

Plist file (tracks.plist) looks like this:

Root  (Dictionary)
  TrackNames (Array)
     Item 0  (String)   Track 1
     Item 1  (String)   Trac开发者_如何学JAVAk 2
     Item 2  (String)   Track 3
     Item 3  (String)   Track 4

and my code:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"tracks" ofType:@"plist"];

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *trackArray = [dict valueForKey:@"TrackNames"];

self.trackData = trackArray;

which is basically the same plist layout and code snippet as that tutorial. If I change the third line to:

NSArray *trackArray = [dict allKeys];

I get the picker populated with "Root", which leads me to believe I'm missing something and not drilling down far enough...

Anyone help?

Thanks


You are loading the plist as a dictionary. That means the Root element is also a key in the dictionary, so to get the track names, you should do this:

NSArray *trackArray = [[dict objectForKey:@"Root"] objectForKey:@"TrackNames"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜