Sheehan Alam Sheehan Alam - 4 months ago 14x
Objective-C Question

How can I get the first element in an NSDictionary?

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;


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];