Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;     } 
like image 760
Sheehan Alam Avatar asked Apr 22 '10 22:04

Sheehan Alam


People also ask

What is NSDictionary in Objective c?

Overview. The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. For example, an interactive form could be represented as a dictionary, with the field names as keys, corresponding to user-entered values.

Does NSDictionary retain objects?

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

How do I know if NSDictionary is empty?

Every number that is 0 equals to @NO . Every number that is not 0 equals to @YES . Therefore you can just pass the count into the if .

What is the difference between NSMapTable vs NSDictionary?

NSDictionary / NSMutableDictionary copies keys, and holds strong references to values. NSMapTable is mutable, without an immutable counterpart. NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.


1 Answers

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]; 
like image 174
Wevah Avatar answered Oct 13 '22 04:10

Wevah