Get Values From NSArray and add it to NSDictionary - ipad

I have NSArray inside the array I have many Dictionaries. I want all those dictionary value into single dictionary
jewels array (
{
headAccObi = "<AccessoriesObject: 0x7f7da00>";
},
{
headAccObi = "<AccessoriesObject: 0x7f7da00>";
noseAccObj = "<AccessoriesObject: 0x7f84e40>";
},
{
headAccObi = "<AccessoriesObject: 0x7f7da00>";
neckAccObj = "<AccessoriesObject: 0x7f7d8c0>";
noseAccObj = "<AccessoriesObject: 0x7f84e40>";
}
)
I want output like this
{
headAccObi = "<AccessoriesObject: 0x7f7da00>";
headAccObi = "<AccessoriesObject: 0x7f7da00>";
noseAccObj = "<AccessoriesObject: 0x7f84e40>";
headAccObi = "<AccessoriesObject: 0x7f7da00>";
neckAccObj = "<AccessoriesObject: 0x7f7d8c0>";
noseAccObj = "<AccessoriesObject: 0x7f84e40>";
}
How can I achieve this?

You can't create exactly the structure you've described because you have the same key used multiple times. Adding an object with setObject:forKey: will replace an existing object if the key is duplicated. You would need to store an array of objects in your dictionary for each key such as headAccObi where there are repeats.

Related

NSDictionary - Get unique records choosing which keys has to be uniques

I have an array of dictionaries with N keys. I want to create an array of unique dicts which contains only some keys.
Example. My dict:
{
"m_anno" = 2017;
"m_c_ultimo" = "4.130";
"m_cod" = 4522;
"m_cod_art" = "*B";
"m_des" = "SCRITTA BIANCA NISSAN";
"m_ditta" = SIS;
"m_prz" = "0.000";
"m_qta" = "1.000";
"m_sconto" = {
};
},
{
"m_anno" = 2017;
"m_c_ultimo" = "25.020";
"m_cod" = 4522;
"m_cod_art" = "000/01200";
"m_des" = "CABLAGGIO X TIMONE";
"m_ditta" = SIS;
"m_prz" = "0.000";
"m_qta" = "1.000";
"m_sconto" = {
};
},
{
"m_anno" = 2017;
"m_c_ultimo" = "1.000";
"m_cod" = 4523;
"m_cod_art" = 000000;
"m_des" = "BLISTER O-RING 3106";
"m_ditta" = SIS;
"m_prz" = "0.000";
"m_qta" = "1.000";
"m_sconto" = {
};
},
I want to get unique values of m_anno and m_cod keys. Expected result:
{
{
"m_anno" = 2017
"m_cod" = 4522
}
{
"m_anno" = 2017
"m_cod" = 4523
}
}
Which is the simplest way?
Basically you have to iterate over the array (quelle surprise!) and put it in a collection, if the pair doesn't exist yet. The second task can be accomplished by using an instance of NSSet or NSOrderedSet depending on the requirement that the order has to be preserved:
NSArray *values = …; // You start with this
NSMutableOrderedSet *uniquePairs = [NSMutableOrderedSet new];
for( NSDictionary *value in values )
{
NSDictionary *pair = #{ #"m_anno":[value objectForKey:#"m_anno"], #"m_cod":[value objectForKey:#"m_cod"] }; // Create smaller version
[uniquePairs addObject:pair];
}
There might be a more elegant way. (Depends of the point of view)
Create an addition to dictionaries, that reduces the array to the desired keys:
NSDictionary( UnifyAnnoAndCodeAddition )
- (NSDictionary*)annoAndCod
{
return #{ #"m_anno":[self objectForkey:#"m_anno"], #"m_cod":[self objectForKey:#"m_code"]};
}
Then use key-value coding to create an array of the reduced dictionaries and make a set of this:
NSArray *values = …; // You start with this
NSArray *pairs = [values valueForKey:#"annoAndCod"];
NSOrderedSet = [NSOrderedSet orderedSetWithArray:pairs];
Well, at least it is shorter.

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 :)

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.

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:

How to extract elements in NSArray using NSPredicate

I have an array (NSArray) with the following content:
dates {
dateOne = {
"date_one" = "2011-11-30";
name = "test1";
dateOne=true
};
dateTwo = {
"date_two" = "2011-11-30";
name = "test2";
dateTwo=false
};
dateThree = {
"date_Three" = "2011-11-30";
dateTwo=true;
name = "test3";
};
the content above is being generating with the following line of code:
NSArray *dates = [myXmlParsed valueForKeyPath:#"myXml.dates"];
using NSLog:
NSLog(#"%#, '%#'", NSStringFromClass([dates class]), dates);
I get the following output:
__NSDictionaryM, '{
dateOne = {
"date_one" = "2011-11-30";
name = "test1";
dateOne=true
};
dateTwo = {
"date_two" = "2011-11-30";
name = "test2";
dateTwo=false
name = "test3";
};
dateThree = {
"date_Three" = "2011-11-30";
name = "test1";
dateTwo=true;
name = "test3";
};
}'
but I'm having problems getting the values of dictionaries inside of the array. My question is how can get the data of dateThree and how can I access to individual node of information for example date_Three ?
I'll really appreciate your help guys
I figure it out. I was using nsarray when I should have done nsmutabledictionary.
NSMutableDictionary *dates=[[NSMutableDictionary alloc] initWithDictionary: [resultDict valueForKeyPath:#"myXml.dates"]];
Now I can query any of the values:
NSLog(#"value %#", [[dates valueForKey:#"dateOne"] valueForKey:#"date_one"]);

Resources