How to store NSDictionary value to an NSArray?
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.
Related videos on Youtube
Stephen Jebakumar
Updated on June 05, 2022Comments
-
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 over 9 yearsThe method
addObject
only works on aNSMutableArray
. This won't work in your example because you are using the immutableNSArray
-
Stephen Jebakumar over 9 yearsThank u! Really appreciate ur help!
-
ZeMoon over 9 yearsYeah I corrected that as soon as I published the answer
-
Jasmeet over 9 years@StephenJebakumar , Thank you stephen, If my suggested answer works, Dont forget to mark this as answer ;) keep coding
-
Badal Shah over 8 yearswhat is result_Array ?
-
Jasmeet over 8 years@BadalShah The Question code snippet has Array. Am referring to that array as result_Array