문제

First please forgive if the title isn't correct terminology. I've searched and searched the questions here and I'm not finding a relevant solution do to the format of my plist. I'm trying to implement the following code and maybe I'm going about it wrong.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"master" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
self.spots = [dict objectForKey:@"Rooms"];

This is the format of my plist and ideally I'd like to pull all "Rooms" into a table from this list.

<plist version="1.0">
 <dict>
   <key>Rooms</key>
   <dict>
     <key>Basement</key>
     <array>
       <string>Stereo</string>
       <string>TV</string>
     </array>
     <key>Bathroom</key>
     <array>
       <string>Lights</string>
     </array>
     <key>BedRoom 1</key>
     <array>
       <string>Blinds</string>
       <string>Lights</string>
       <string>Stereo</string>
       <string>TV</string>
     </array>

I've tried using [dict allKeys] which resulted in an unsorted result only after I changed the format of my plist. My problem should be obvious, I can't get past the "Rooms" key and list the keys nested in the dictionary. What's wrong here? My code? My plist? I've read for hours and I'd like to see a relevant example that I'll be able to pull apart and learn or explanation in plain English if someone feels like just posting a link or saying just use.....

도움이 되었습니까?

해결책

So looking at your code, self.spots should be a dictionary of rooms. I think you are asking how do you then get an ordered list of keys so you can present them alphabetically?

NSArray * sortedKeys = [[self.spots allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

You can then use the sorted keys to access the Dictionaries in the correct order

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top