Group NSDictionary by dates [duplicate] - ios

This question already has answers here:
Grouping NSArray of NSDictionary based on a key in NSDictionay
(2 answers)
Closed 8 years ago.
I have a NSDictionary and i want to group it creating an array of objects by date
Example of the main NSDictionary:
({
date = "2014-04-27";
group = "yellow.png";
length = 180;
},
{
date = "2014-04-28";
group = "blue.png";
length = 180;
},
{
date = "2014-04-27";
group = "blue.png";
length = 120;
})
I want to group something similar as:
2014-04-27 = (
{
date = "2014-04-27";
group = "yellow.png";
length = 180;
},
{
date = "2014-04-27";
group = "blue.png";
length = 180;
})
2014-04-28 = ( {
date = "2014-04-28";
group = "blue.png";
length = 120;
})
Could someone help me? i have tried many FOR but i cant get it

It appears as though your original data structure is an array of dictionaries. Was your question phrased incorrectly? I see each individual dictionary but they are not keyed on anything in the top level data structure.
Assuming that is the case (you have an array called originalArray
NSMutableDictionary *dictionaryByDate = [NSMutableDictionary new];
for(NSDictionary *dictionary in originalArray)
{
NSString *dateString = dictionary[#"date"];
NSMutableArray *arrayWithSameDate = dictionaryByDate[dateString];
if(! arrayWithSameDate)
{
arrayWithSameDate = [NSMutableArray new];
dictionaryByDate[dateString] = arrayWithSameDate;
}
[arrayWithSameDate addObject: dictionary];
}
By the end of this, dictionaryByDate will be a dictionary (keyed on date) of arrays (all objects in a given array will be dictionaries with the same date).

Related

Getting values from key in NSDictionary

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

Save multiple values with same keys in NSMutableDictionary then store the dictionary in NSMutableArray

I know this question has been asked many times but not got the solution or i might not understood that's why posting the question.
for (int web=0; web<[web_arr count]; web++) {
AdditionalBookmark *web_book=[[AdditionalBookmark alloc]init];
UITextField *txtNam = (UITextField*)[self.view viewWithTag:1100+web];
NSString *txtName = txtNam.text;
UITextField *txtLin = (UITextField*)[self.view viewWithTag:1200+web];
NSString *txtLink = txtLin.text;
if ((txtName && txtName.length > 0)||(txtLink && txtLink.length > 0))
{
web_book.additionalBookmark_Name = txtName;
web_book.additionalBookmark_Link= txtLink;
web_book.contactDetails_Id=self.contactDetailsinfo.contactDetailsId;
web_book.additionalBookmark_Id=[[[web_arr objectAtIndex:web] valueForKey:#"WeblinkId"] intValue];
[dictWebLinks setObject:txtName forKey:#"WeblinkName"];
[dictWebLinks setObject:txtLink forKey:#"WeblinkUrl"];
[arrForWebLinks addObject:dictWebLinks];
}
[db updateIntoAdditionalBookmarkWithObject:web_book];
}
[dictUpdate setObject:arrForWebLinks forKey:#"Weblink"];
The problem am facing is this suppose there are two items in the array
Printing description of web_arr:
<__NSArrayM 0x170e4db60>(
{
WeblinkId = 9;
WeblinkName = Hdhd;
WeblinkUrl = "ie.comh";
},
{
WeblinkId = 10;
WeblinkName = Hd;
WeblinkUrl = "nd.com";
}
)
what i am getting is below:
Printing description of arrForWebLinks:
<__NSArrayM 0x170e4db30>(
{
WeblinkName = Hd;
WeblinkUrl = "nd.com";
},
{
WeblinkName = Hd;
WeblinkUrl = "nd.com";
}
)
dictionary is replacing the value for same key holding the latest value only, how can i mange to save the values in same format as in web_arr.
Any help is appreciable.
You are adding the same dictionary to the array over and over:
[dictWebLinks setObject:txtName forKey:#"WeblinkName"];
[dictWebLinks setObject:txtLink forKey:#"WeblinkUrl"];
[arrForWebLinks addObject:dictWebLinks];
You need to create a new one each time:
[arrForWebLinks addObject:#{
#"WeblinkName" : txtName,
#"WeblinkUrl" : txtLink
}];
(and remove dictWebLinks).

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.

How to find common value from two NSArray? [duplicate]

This question already has answers here:
Finding Intersection of NSMutableArrays
(5 answers)
Closed 9 years ago.
In my project I have two NSArray and both array contains two values frequency and key.
Now I have to compare these two NSArray with the reference of key, then I have to find common key and from this common key I have to store frequencies of each array in another array so that I have common count for each array
Example
Printing description of xSeriesArray:
{
frequency = 60;
key = 5591090;
},
{
frequency = 50;
key = 5591089;
},
{
frequency = 40;
key = 5591082;
},
{
frequency = 30;
key = 5591078;
},
{
frequency = 20;
key = 5591077;
},
{
frequency = 10;
key = 5591076;
}
Printing description of ySeriesArray:
<__NSArrayM 0xa1e1270>
{
frequency = 500;
key = 5591089;
},
{
frequency = 400;
key = 5591082;
},
{
frequency = 300;
key = 5591078;
},
{
frequency = 200;
key = 5591077;
},
{
frequency = 100;
key = 5591076;
}
On Above array data in 1st array I have 6 count and in another I have 5 count
Please help me to find common key from these two NSArray
Used Set this is simplest way to find common values.
NSMutableSet* set1 = [NSMutableSet setWithArray:yourFirstArray];
NSMutableSet* set2 = [NSMutableSet setWithArray:yourSecondArray];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets
NSArray* result = [set1 allObjects];
Check Swift code:-
var set1 = Set<AnyHashable>(yourFirstArray)
var set2 = Set<AnyHashable>(yourSecondArray)
set1.intersect(set2) //this will give you only the obejcts that are in both sets
let result = Array(set1)

how to sort array of NSDictionary [duplicate]

This question already has answers here:
How can I sort an NSArray containing NSDictionaries?
(3 answers)
Closed 9 years ago.
i have array of NSDictionary like below
array(
{
Birthdate = "03/20";
Names = "Abhinavswati Chaudhary";
remain = 0;
},
{
Birthdate = "09/22/1986";
Names = "Aisa Soni";
remain = 186;
},
{
Birthdate = "03/02/1990";
Names = "Anuj Parekh";
remain = 347;
},
{
Birthdate = "05/22/1988";
Names = "Ajay Desai";
remain = 63;
}
)
how can i sort this array in ascending with accordance to remain
for example it should be like
array(
{
birthdate=...
name =abhivan
remanin =0;
},
{
brithdate = "05/22/1988";
name =ajay
remain =63
},
{
and so on....
plz suggest some code thank you in advance
when we use NSsortDiscripor then remain need to be NSNumber, can it be the reason that my remain numbers are actually string ?
This is quite simple by using the sortedArrayUsingComparator method of NSArray.
In your case it should look like:
yourArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
// get values
NSNumber *remain1 = obj1[#"remain"];
NSNumber *remain2 = obj2[#"remain"];
return [remain1 compare:remain2];
}];

Resources