39

I have an array of NSDictionaries. How can I pull out the first element in the dictionary?

   NSArray *messages = [[results objectForKey:@"messages"] valueForKey:@"message"];         
    for (NSDictionary *message in messages)
    {
        STObject *mySTObject = [[STObject alloc] init];
        mySTObject.stID = [message valueForKey:@"id"];      
        stID = mySTObject.stID;
    }
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

8 Answers8

62

There is no "first" element in an NSDictionary; its members have no guaranteed order. If you just want one object from a dictionary, but don't care which key it's associated with, you can do:

id val = nil;
NSArray *values = [yourDict allValues];

if ([values count] != 0)
    val = [values objectAtIndex:0];
Wevah
  • 28,182
  • 7
  • 83
  • 72
  • 1
    Yes, so you can still message `val` without crashing if `[values count]` is zero. – Wevah Sep 28 '12 at 23:41
  • 3
    Please be aware that the ```allValues``` method returns an unordered array. Per the documentation "The order of the values in the array isn’t defined.". So you may not get the object you are expecting to get. – Jeremy Fox Jun 18 '13 at 14:20
  • You can use subscripts too. `val = values[0];` is legal. More on subscripts here: http://clang.llvm.org/docs/ObjectiveCLiterals.html – cbh2000 Nov 06 '13 at 22:12
  • `allValues` doesn't guarantee order. – mikekavouras Oct 13 '15 at 23:57
25

NSDictionaries are unordered, meaning that there are not first or last element. In fact, the order of the keys are never guaranteed to be the same, even in the lifetime of a specific dictionary.

If you want any object, you can get one of the keys:

id key = [[message allKeys] objectAtIndex:0]; // Assumes 'message' is not empty
id object = [message objectForKey:key];
Martin Cote
  • 28,864
  • 15
  • 75
  • 99
6

NSArray has a selector named firstObject that simplifies the code and makes it more readable:

id val = [[yourDict allValues] firstObject];

If yourDict is empty val will be nil, so is not necessary to check the dictionary/array size.

ASLLOP
  • 174
  • 2
  • 12
  • 1
    While that's true, and saves a line or two of code, the question is about `NSDictionary`, not `NSArray`. – trojanfoe May 26 '16 at 14:53
  • Yes, and is the "first" value of the dictionary what you get with this code. Is just that it uses an array conversion, like almost all other answers. – ASLLOP May 26 '16 at 20:17
  • There is no "first" with a dictionary as it has no order. You will potentially get a different "first" each time you add an object to the dictionary. – trojanfoe May 26 '16 at 21:39
  • Of course, a dictionary is not an array, there is no implicit order on it. But the user doesn't asked for a specific order on the data. – ASLLOP Jun 15 '16 at 08:42
3

Simplest:

[[dict objectEnumerator] nextObject];
Ivan Caravanio
  • 597
  • 1
  • 7
  • 20
2

According to Apple, calls to allKeys or allValues incur the cost of creating new arrays:

A new array containing the dictionary’s values, or an empty array if the dictionary has no entries (read-only)

So, an alternative option that does not incur such cost could look like this:

NSString* key = nil;
for(key in yourDict)
{ // this loop will not execute if the dictionary is empty
   break; // exit loop as soon as we enter it (key will be set to some key)
}
id object = yourDict[key]; // get object associated with key. nil if key doesn't exist.

Note: If the dictionary is empty, the key will remain nil, and the object returned will also be nil, we therefore don't need special handling of the case where the dictionary is actually empty.

LZM
  • 169
  • 1
  • 8
2

If someone is still looking for answer for this type of situation then can refer this:

// dict is NSDictionary

// [dict allKeys] will give all the keys in dict present

// [[dict allKeys]objectAtIndex:0] will give from all the keys object at index 0 because [dict allKeys] returns an array.

[[dict allKeys]objectAtIndex:0];
1218GG
  • 393
  • 3
  • 11
0

If you have NSDictionary named message, It's pretty simple:

message[[[message allKeys] objectAtIndex:0]];

But you have to be sure (or better check) that your dictionary has at least one element. Here is how you can check it:

if ([message allKeys] > 0) NSLog(@"%@", message[[[message allKeys] objectAtIndex:0]]);

But NSDictionary has no guaranteed order, so you probably should use this code only if your dictionary has only one element.

[UPDATE] It's also good idea to use this if you need to get ANY element of dictionary

n0_quarter
  • 538
  • 1
  • 10
  • 23
-2

Try this:

NSDictionary *firstValue = [responseObject objectAtIndex:0];
Tea
  • 306
  • 4
  • 8