Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary - how return the Key as a string

I have a plist like this:

<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 the object I would write something like this:

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

what about if I want to get "overview" from my plist??? I Hope it's clear..... Please do not give negative vote.... I'm still learning!;-)

EDITED 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 the Apple sample code: http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html

In this sample the string play return the name of the play in the header section but I want to modify it and get the "Key" in the header (in this case would be "PlayName).

THANKS TO EVERYBODY: THIS IS HOW I FIXED IT:

 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;
like image 320
Mat Avatar asked Nov 28 '11 06:11

Mat


People also ask

How do you set a value in NSDictionary?

You have to convert NSDictionary to NSMutableDictionary . You have to user NSMutableDictionary in place of the NSDictionary . After that you can able to change value in NSMutableDictionary .

What is NSMutableDictionary?

An object representing a dynamic collection of key-value pairs, for use instead of a Dictionary variable in cases that require reference semantics.

Does NSDictionary retain objects?

An NSDictionary will retain it's objects, and copy it's keys.

How do I create an NSDictionary in Objective C?

Creating NSDictionary Objects Using Dictionary Literals In addition to the provided initializers, such as init(objects:forKeys:) , you can create an NSDictionary object using a dictionary literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:forKeys:count:) method.


2 Answers

You can get an array of all the keys in the 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 like this:

for (NSString *key in dict) {
    ...
}
like image 124
rob mayoff Avatar answered Oct 07 '22 04:10

rob mayoff


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;
like image 42
Ankit Srivastava Avatar answered Oct 07 '22 04:10

Ankit Srivastava