0

I want to recover the values contained in a NSDictionary. I do this :

MyCommon *pCommon = [MyCommon Singleton];
NSArray *result = pCommon.res; //res is define and retain in common.m. This is the array.
NSDictionary *dataItem = [result objectAtIndex:????];

i don't know what i can put to replace the ????

Thanks

2 Answers2

2

Unlike some other languages, Objective-C makes a distinction between Arrays and Dictionaries.

An array is a collection, indexed by number. A dictionary is a collection indexed by objects (commonly strings, but can be other types).

On top of that, Objective-C makes a distinctions between mutable and immutable arrays and dictionaries. NSArray and NSDictionary cannot be modified after they are created. If you need to add objects to your collection, use NSMutableArray and NSMutableDictionary instead.

NSArray * myArray = [[NSArray alloc] initWithObjects: @"text1", @"text2", @"text3", nil];
// you can then access a member by using:
NSString * text = [myArray objectAtIndex: 0];

NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
// add an object
[myDict setObject:@"text1" forKey:@"key1"];
[myDict setObject:@"text2" forKey:@"key2"];
// you can retrieve objects using:
text = [myDict objectForKey: @"key1"];

[myArray release];
[myDict release];
nash
  • 2,181
  • 15
  • 16
0

Well, result is an NSArray, not an NSDictionary, so just put the index of the element you are interested in. Arrays are indexed by integers from zero.

In case your result is an NSDictionary and not an NSArray, use the objectForKey: method instead of objectAtIndex:.

Tamás
  • 47,239
  • 12
  • 105
  • 124