Stephen Jebakumar
Stephen Jebakumar

Reputation: 23

How to store NSDictionary value to an NSArray?

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;
},

Upvotes: 2

Views: 12661

Answers (7)

Rocky
Rocky

Reputation: 3245

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.

Upvotes: 2

ZeMoon
ZeMoon

Reputation: 20284

Thw following example will do the same with the least amount of code.

NSMutableArray *arr = [NSMutableArray new];

for (NSDictionary* dict in myArray)
{
    [arr addObject:[dict valueForKey:@"title"]];
}

The arr object will have all values for the key 'title'.

Upvotes: 0

longbow
longbow

Reputation: 1623

If you are getting "an array of stuff", I would not store it in a Dict I would store it to an NSArray and then iterate over it like:

var myObjects:[Object]

var myCollection = yourJSON as NSArray (assuming that you always get the same.

func myStuff (myCollection) {
    for item in myCollection {
    change = myCollection["changed"] as NSString (assuming a String here)
    city = myCollection["city"] as NSString
    ...
    ...

var Foo:Object = Object()
Foo.change = change
Foo.city = city
...
...


myObjects.append(Foo)
}

later on you can now easily iterate the myObjects Array to present stuff in a TableView

Upvotes: 0

user3182143
user3182143

Reputation: 9609

Easy steps

 NSMutableArray *array = [yourDict copy];[If it is your dict is NSMutableDictionary]
     or
 NSMutableArray *array = [yourDict mutableCopy]; [If it is your dict is NSDictionary];

 for(int i=0;i<[array count];i++)
 {

   NSString *strTitle = [NSString stringWithFormat:@"%@",[[array objectAtIndex:i]valueForKey:@"title"]];
   NSLog(@"the title is==%@",strTitle);

 }

Upvotes: 0

user3182143
user3182143

Reputation: 9609

Please do the following steps for your answer

 1.First of all You should Create NSMutableArray
 2.After copy the dictionary to NSMutableArray
    NSMutableArray *array = [yourDictionary copy];
 3.After that set or create the for loop.
 4.Create NSString *strTitle in for loop.
 5.Finally strTitle = [NSString stringWithFormat:@"%@",[[array objectAtIndex:i]valueForKey:@"title"]]];

Upvotes: 0

Jasmeet
Jasmeet

Reputation: 1531

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

Upvotes: 5

Shaheen Ghiassy
Shaheen Ghiassy

Reputation: 7517

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.

Upvotes: 1

Related Questions