Getting values from key in NSDictionary - ios

I'm using the Edmunds API to return a JSON string of vehicle makes for a certain year.
The resulting NSDictionary looks like this:
makes = (
{
id = 200002038;
models = (
{ // not relevant
}
);
name = Acura;
niceName = acura;
},
{
id = 200001769;
models = (
{ // not relevant
}
);
name = "Aston Martin";
niceName = "aston-martin";
},
There are a lot more values in this dictionary...how can I loop through through the dictionary to retrieve each of the 'name' values?

I like valueForKeyPath because you can do so much with it and it's a one line solution. Tested this and it works in my dev setup. If you know your data is in a predictable format, then you can do amazing things with valueForKeyPath.
NSArray *makeNames = [makes valueForKeyPath:#"name"];
Great info on valueForKeyPath http://nshipster.com/kvc-collection-operators/

NSMutableArray *makeNames = [NSMutableArray new];
for (NSDictionary *make in makes) {
NSLog(#"Make name: %#", make[#"name"]);
[makeNames addObject:make];
}
NSLog(#"Makes array: %#", makeNames);

Related

How to put NSDictionary inside NSDictionary into UITableView?

I receive following NSDictionary from my server. I don't know how to access each row and put them in UITableView. I need a for loop or something to access each of them one by one: I get following when I say:
NSLog(#"%#",dict);
{
chores = (
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 1;
name = TEST;
parent = Blah;
"previous_chores" = (
);
},
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 2;
name = TEST2;
parent = Blah;
"previous_chores" = (
);
},
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 3;
name = TEST3;
parent = Blah;
"previous_chores" = (
);
}
);
}
I guess you can directly use the array without split it like following:
Suppose you have an array with dictionary and inside the dictionary as your data and kiran said:
NSArray *arrChores = [dict valueForKey #"Chores"];
Your array look like 0th index is:
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 1;
name = TEST;
parent = Blah;
"previous_chores" = (
);
}
so you can print the name like following in cellForrowIndex:
NSLog(#"=== %#",[[arrChores objectAtIndex:indexPath.row]valueForKey:#"name"])
More easy and less maintain :)
Create a model for Chores.
Looking at the dictionary.
Chores is an array consisting of dictionaries in it.
So you can have array like this
arrChoresGlobal = [NSMutableArray new];
NSArray *arrChores = [dict valueForKey #"Chores"];
Then iterate this array and have a model.
like below
for(NSDictionary *dctChore in arrChores)
{
SomeModel *obj = [SomeModel new];
obj.first_due_date = [dctChore valueForKey:#"first_due_date"];
[arrChoresGlobal addObject:obj];
}
Then use this array count in numberOfRows in tableview
return [arrChoresGlobal count];
and in cellForRowAtIndexPath
SomeModel *obj = [arrChoresGlobal objectAtIndex:indexPath.row];
Edit :- Longer but systematic way :)

Parsing Json Output correctly

I am trying to correctly target the elements within the Json Output and I am getting closer but I presume there is a easy and obvious way I am missing.
My Json looks like this with a upper level event.
JSON SNIPPET UPDATED
chat = (
(
{
Key = senderId;
Value = {
Type = 0;
Value = "eu-west-1:91afbc3f-890a-4160-8903-688bf0e9efe8";
};
},
{
Key = chatId;
Value = {
Type = 0;
Value = "eu-west-1:be6457ce-bac1-412d-9307-e375e52e22ff";
};
},
{
Key = timestamp;
Value = {
Type = 1;
Value = 1430431197;
};
},
//Continued
I am targeting this level using
NSArray *chat = array[#"chat"];
for ( NSDictionary *theCourse in chat )
{
NSLog(#"---- %#", theCourse);
// I tried the following to target the values
//NSLog(#"chatId: %#", [theCourse valueForKey:#"Key"]);
//NSLog(#"timestamp: %#", theCourse[#"senderId"] );
}
}
I need to parse the value data for each key which if I was using an array would do like [theCourse valueForKey:#"Key"] but I think I may not be going deep enough?
As you would expect, [theCourse valueForKey:#"Key"] gives me the Key values but I need the associate values of those keys.
You can create an easier dictionary:
NSArray *chat = array[#"chat"][0];
NSMutableDictionary* newDict = [NSMutableDictionary dictionary];
for (NSDictionary* d in chat)
[newDict setValue:d[#"Value"][#"Value"] forKey:d[#"Key"]];
Now you can use the newDict.
NSLog(#"chatId: %#", [newDict valueForKey:#"chatId"]);

Cannot access key in dictionary

I have an NSDictionary and am trying to access the 'venue' key and assign various keys such as name. I've tried to take the dictionary and access venue with no luck:
NSDictionary *dic = result;
NSArray *venues1 = [dic valueForKeyPath:#"response.groups.items.venue"];
How would I access venue?
{
meta = {
code = 200;
};
response = {
groups = (
{
items = (
{
venue = {
name = "Ping Tom Memorial Park";
you can access it in this way
NSDictionary *venues1 = dic[#"response"][#"groups"][0][#"items"][0][#"venue"];
be careful that groups, items are arrays and venue is a dictionary instead of array.

NSDictionary - need to obtain value for key subelement

I have created an NSDictionary named "myData"
which contains the following JSON response:
{
listInfo = (
{
date = 1392157366000;
dateAsString = "02/11/2014 22:22:46";
id = 6;
address = 542160e0000c;
myLevel = 13;
},
{
date = 1392155568000;
dateAsString = "02/11/2014 21:52:48";
id = 5;
address = 542160e0000c;
myLevel = 13;
}
);
}
I need to retrieve each of the [dateAsString] key/value pairs.
I've tried: NSString *dateAsString=[[myData valueForKeyPath:#"dateAsString"][0] objectForKey:#"myData"]; without any luck.
Any suggestions are greatly appreciated.
I think this will work:
NSArray* dateStringArray = [listInfo valueForKeyPath:#"#unionOfObjects.dateAsString"]
I believe it will give you an array of strings. If you need to stuff that back in a dictionary, that should be fairly easy.
It's not clear what your "myData" looks like... so I used the listInfo array of dicts shown.

Get all similar values of key of bunch of (NSDictionary)s within an NSMuttableArray

I have a NSMuttableArray having values like
(
{
MedicineAlarmTime = "18:00:00";
MedicineDateTime = "24/04/2014 16:00:09";
},
{
MedicineAlarmTime = "18:00:00";
MedicineDateTime = "24/04/2014 16:00:26";
},
{
MedicineAlarmTime = "19:00:00";
MedicineDateTime = "24/04/2014 16:00:26";
},
{
MedicineAlarmTime = "19:00:00";
MedicineDateTime = "24/04/2014 16:00:26";
}
)
Is it possible to retrieve arrays of similar "MedicineAlarmTime" like
array1 = (
{
MedicineAlarmTime = "18:00:00";
MedicineDateTime = "24/04/2014 16:00:09";
},
{
MedicineAlarmTime = "18:00:00";
MedicineDateTime = "24/04/2014 16:00:26";
}
)
array2 = (
{
MedicineAlarmTime = "19:00:00"
MedicineDateTime = "24/04/2014 16:00:09";
},
{
MedicineAlarmTime = "19:00:00";
MedicineDateTime = "24/04/2014 16:00:26";
}
)
NSMutableDictionary *groupedAlarms = [NSMutableDictionary dictionary];
for (NSDictionary *alarm in originalArray) {
NSString *alarmTime = [alarm objectForKey: MedicineAlarmTime];
NSMutableArray *alarmGroup = [groupedAlarms objectForKey: MedicineAlarmTime]
if (alarmGroup) {
[alarmGroup addObject: alarm];
} else {
alarmGroup = [NSMutableArray arrayWithObjects: alarm, nil];
[groupedAlarms setValue:alarmGroup forKey:alarmTime];
}
}
This creates an NSMutableDictionary, groupedAlarms. The keys in groupedAlarms are the alarm times from the dictionaries in the original array--the actual alarm time value. The values for each key are arrays. The arrays contain the dictionaries of the original array--sorted based on the alarm time (which is used as the key to the array which contains them).
If you want the "alarmGroup" from the above example to be more general and not group exact matches only, you can use the same forin loop structure. The checks to determine whether to put it in an existing group or create a new group become slightly more complex, but the important thing is, we still don't need to presort the array and then iterate through it a second time.
The only reason I can see for pre-sorting would be if you had a set number of groups you wanted, and a set number of objects in each group. Then you can't really know what group to put each object in until the array has been sorted.
But here, we're trying to put objects in groups with similar object, so the algorithm is simple:
Grab an object. See if it belongs in an existing group. If not, create a new group for this object. Move to the next object.
And for any downvoters... I can't really provide example code for a more complex example than the existing sample code in this answer unless the original question is made more clear.
Sure. You just have to write code for it.
First, you use the NSArray method sortedArrayUsingDescriptor: to get an array that is sorted by MedicineAlarmTime, so array elements that you might want together will actually be together. Then you iterate through the array, comparing the MedicineAlarmTime to check if two array elements are "similar" enough for you, and create arrays with those groups, using the NSArray method subArrayWithRange:

Resources