How to store NSDictionary value to an NSArray?

12,464

Solution 1

The above mentioned snippet is an array, lets name it "result_Array". In "result_Array" you are showing two objects. And each object is further a dictionary. And from each Dictionary you want to fetch title and save it in your array named "data". Here we go

NSMutableArray *data=[NSMutableArray new];

for(int i=0; i<result_Array.count ; i++)
{
    NSDictionary *dict = [result_Array objectAtIndex:i];
    [data addObject:[dict objectForKey:@"title"]];
}

Hope it helps. Feel free to ask any query

Solution 2

If you have an array of NSDictionary, then you can get all values of a key using method "valueForKey" without any iteration:

NSArray *list = @[

@{@"changed":@"1414164684",
@"title":@"19 Glas Bar & Matsal"},

@{@"changed":@"1413991969",
@"title":@"28+"}];

NSArray *titles = [list valueForKey:"title"];

In the array titles you get only titles from list.

Solution 3

The example code you provided is actually an Array of Dictionaries. So to answer your question with that small change

NSArray *originalDict;
NSMutableArray *data = [NSMutableArray new];
for (NSUInteger i = 0; i < originalDict.count; i++) {
    NSDictionary *currentDictionaryPointer = [originalDict objectAtIndex:i];
    NSString *title = [currentDictionaryPointer objectForKey:@"title"];
    [data addObject:title];
 }

Where originalDict is the object that points to your provided sample code.

Share:
12,464

Related videos on Youtube

Stephen Jebakumar
Author by

Stephen Jebakumar

Updated on June 05, 2022

Comments

  • Stephen Jebakumar
    Stephen Jebakumar almost 2 years

    In a dictionary I have Key & values like below. How can I get "title" values from the below and store it in a NSArray *data?

    {
        changed = 1414164684;
        city = Stockholm;
        class = "3. Mycket god klass";
        coordinates = "POINT(59.3246206 18.0686084)";
        id = 37510;
        title = "19 Glas Bar & Matsal";
        total = 70;`enter code here`
    },
    {
        changed = 1413991969;
        city = "G\U00f6teborg";
        class = "2. M\U00e4starklass";
        coordinates = "POINT(57.697944330446234 11.974067687988281)";
        id = 34865;
        title = "28+";
        total = 77;
    },
    
  • Shaheen Ghiassy
    Shaheen Ghiassy over 9 years
    The method addObject only works on a NSMutableArray. This won't work in your example because you are using the immutable NSArray
  • Stephen Jebakumar
    Stephen Jebakumar over 9 years
    Thank u! Really appreciate ur help!
  • ZeMoon
    ZeMoon over 9 years
    Yeah I corrected that as soon as I published the answer
  • Jasmeet
    Jasmeet over 9 years
    @StephenJebakumar , Thank you stephen, If my suggested answer works, Dont forget to mark this as answer ;) keep coding
  • Badal Shah
    Badal Shah over 8 years
    what is result_Array ?
  • Jasmeet
    Jasmeet over 8 years
    @BadalShah The Question code snippet has Array. Am referring to that array as result_Array