NSDictionary - how to return a key as a string

I have such a plist:

<dict> <key>New item</key> <dict> <key>document</key> <string>driving licence</string> <key>Overview</key> <string>a driving licence is required.......</string> </dict> 

If I want to get an object, I would write something like this:

  myString = [somedictionary objectForKey:@"Overview"]; 

But what if I want to get a "review" from my plist ??? I hope this is clear ..... Please do not give a negative vote .... I'm still learning! -)

UNITED VERSION:

Better to be more specific:

I have this code

  for (NSDictionary *playDictionary in playDictionariesArray) { Play *play = [[Play alloc] init]; play.name = [playDictionary objectForKey:@"playName"]; 

which is sample Apple code: http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html

In this example, playing the string returns the name of the playback in the header section, but I want to change it and get the β€œkey” in the title (in this case it will be β€œPlayName”).

THANKS TO EVERYONE: THIS HOW I SAID THIS:

  NSMutableArray *anArray = [[NSMutableArray alloc] init]; [anArray addObject:@"Overview"]; [anArray addObject:@"pre-requirements"]; [anArray addObject:@"where"]; [anArray addObject:@"what"]; //Use a for each loop to iterate through the array for (NSString *s in anArray) { Play *play = [[Play alloc] init]; play.name = s; 
+4
source share
3 answers

You can get an array of all keys in a dictionary using [dict allKeys] . You can get an array of all keys whose value is a specific object using [dict allKeysForObject:object] . You can process the keys one by one in a loop as follows:

 for (NSString *key in dict) { ... } 
+16
source

try this and if it works let me know ..

 NSMutableArray* result = [NSMutableArray array]; for (NSString* key in dictionary) { [result addObject:key]; // write to a dictionary instead of an array // if you want to keep the keys too. } return result; 
+3
source

If you want to get the key for this object, you can try the method:

 - (NSArray *)allKeysForObject:(id)anObject 

Using it as follows:

 myString = [[myDict allKeysForObject:@"a driving licence is required......."] objectAtIndex:0]; 

Just note that it returns an NSArray, because you may have multiple keys for the specified object.

0
source

Source: https://habr.com/ru/post/1383379/


All Articles