NSDictionary: Find a specific object and remove it - ios

I have NSMutableDictionary that looks like this:
category = (
{
description = (
{
id = 1;
name = Apple;
},
{
id = 5;
name = Pear;
},
{
id = 12;
name = Orange;
}
);
id = 2;
name = Fruits;
},
{
description = (
{
id = 4;
name = Milk;
},
{
id = 7;
name = Tea;
}
);
id = 5;
name = Drinks;
}
);
Now, when a user performs an action in my application, I get the #"name"-value of the object, like "Apple". I would like to remove that object from the dictionary, but how will can I reach this object with the
[myDictionary removeObjectForKey:]
method?

At a high level you need to get a reference to the "description" array. Then iterate through the array getting each dictionary. Check the dictionary to see if it has the matching "name" value. If so, remove that dictionary from the "description" array.
NSString *name = ... // the name to find and remove
NSMutableArray *description = ... // the description array to search
for (NSUInteger i = 0; i < description.count; i++) {
NSDictionary *data = description[i];
if ([data[#"name"] isEqualToString:name]) {
[description removeObjectAtIndex:i];
break;
}
}

To remove something from an INNER array:
NSString* someFood = <search argument>;
NSArray* categories = [myDictionary objectForKey:#"category"];
for (NSDictionary* category in categories) {
NSArray* descriptions = [category objectForKey:#"description"];
for (int i = descriptions.count-1; i >= 0; i--) {
NSDictionary* description = [descriptions objectForIndex:i];
if ([[description objectForKey:#"name"] isEqualToString:someFood]) {
[descriptions removeObjectAtIndex:i];
}
}
}
To remove an entire group (eg, "Fruits") from the outer array is simpler.

Related

Group element in NSMutableArray which contains object

I have an NSMutableArray that contains an object of a class model in each position like this.
The class model contains 2 types of information, which we will call id and name.
So, in every location of my NSMutableArray I have an object that contains 2 information.
Then, in the first position of my NSMutableArray I have
{
id = 1;
name = "Dan"; //this is the first object in NSMutableArray
}
In the second position of NSMutableArray, I have:
{
id = 1;
name = "Luca";
}
In the third position
{
id = 2;
name = "Tom";
}
and so on..
Ok, my goal is to make the union of identical IDs between the various objects within the SNMutableArray but it's too difficult!
For example, if I have:
{
id = 1;
name = "Tom";
}
{
id = 1;
name = "Luca";
}
{
id = 2;
name = "Steve";
}
{
id = 2;
name = "Jhon";
}
{
id = 3;
name = "Andrew";
}
The goal is:
{
id = 1;
name = "Tom";
name = "Luca";
}
{
id = 2;
name = "Steve";
name = "Jhon";
}
{
id = 3;
name = "Andrew";
}
Any ideas? would like to use this in the cellForRowAtIndexPath method and I tried to write this: (cm is my class model and myArray is the NSMutableArray which contains an object of cm class)
ClassModel *cm = [myArray objectAtIndex:indexPath.row];
NSMutableArray * resultArray = [NSMutableArray new];
NSArray * groups = [array valueForKeyPath:cm.ID];
for (NSString * groupId in groups)
{
     NSMutableDictionary * entry = [NSMutableDictionary new];
     [insert setObject: groupId forKey: # "groupId"];
     NSArray * groupNames = [array filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: # "groupId =% #", groupId]];
     for (int i = 0; i <groupNames.count; i ++)
     {
         NSString * name = [[groupNames objectAtIndex: i] objectForKey: # "name"];
         [entry setObject: name forKey: [NSS string stringWithFormat: # "name% d", i + 1]];
     }
     [resultArray addObject: entry];
}
NSLog (# "% #", resultArray);
But this does not work..maybe because each element in my array is an object?? .. Help!
You have the right basic idea, but you shouldn't try and do this in cellForRowAt. Rather, you need to create a new array that has the data in the required structure and use that array as the source for your tableview. You will also need to create a new class to put in the array; one that has an id and an NSMutableArray for the names (I won't show this but I will call it GroupClassModel)
Use something like:
NSMutableDictionary *groups = [NSMutableDictionary new]
for (ClassModel *cm in array) {
GroupClassModel *gcm = groups[cm.id];
if (gcm == nil) {
gcm = [GroupClassModel new];
gcm.id = cm.id
groups[cm.id] = gcm
}
[gcm.names addObject:cm.name];
}
NSArray *groupedName = [groups allValues];
// Finally, sort groupedName by id if that is required.

How to compare two nsarrays with different values?

Hi i have two nsarrays.
Array A:
arrProductSelection = [[NSArray alloc]initWithObjects:#"English",#"German",#"Russian",#"Chinese",#"Spanish",#"French",#"French",#"French",#"French",#"French",#"French",#"French",#"French",nil];
Array B:
arrProductSelectionB = [[NSArray alloc]initWithObjects:#"deselcted",#"selected",#"selected",#"selected",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",nil];
I need to compare two arrays and get the value from array A by comparing with array B having value as selected. That is i should get german,chinese and russian sepearted by comma as nsstring.
Try this:
NSMutableArray *arrSelected = [[NSMutableArray alloc] init];
NSArray *arrProductSelection = [[NSArray alloc]initWithObjects:#"English",#"German",#"Russian",#"Chinese",#"Spanish",#"French",#"French",#"French",#"French",#"French",#"French",#"French",#"French",nil];
NSArray *arrProductSelectionB = [[NSArray alloc]initWithObjects:#"deselcted",#"selected",#"selected",#"selected",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",nil];
for(int i = 0; i< arrProductSelectionB.count-1;i ++) {
if ([arrProductSelectionB[i] isEqualToString:#"selected"]) {
[arrSelected addObject:arrProductSelection[i]];
}
}
NSString *strSelected = [arrSelected componentsJoinedByString:#","];
NSLog(#"%#", strSelected);//output: German,Russian,Chinese
If you can make this type of array then manage easly.
(
{
flag = deselcted;
name = English;
},
{
flag = selected;
name = German;
},
{
flag = deselcted;
name = Russian;
},
{
flag = deselcted;
name = Chinese;
}
)
==> Array[(dictionary),(dictionary),.....]
if you need result in an array, Here is the function:
NSMutableArray*resultArray=[[NSMutableArray alloc]init];
for(int i=0; i<arrProductSelectionB.count;i++){
if ([[arrProductSelectionB objectAtIndex:i]isEqualToString:#"selected"]) {
[resultArray addObject:[arrProductSelection objectAtIndex:i]];
}
}
resultArrayhave the values which you need.
first i would make sure both arrays have same counters for safety something like
if (arrProductSelection.count == arrProductSelectionB) {
//than all you need is one for cycle something like :
for (int i = 0; i < arrProductionSelectionB.count; i++) {
if ([arrProductionSelectionB[i] isEqualToString: #"selected"]) {
do something magically with arrProductSelection[i];
}
}
}

How to get JSON response array all index values?

I need to get JSON response array all indexes values and maintain separate array. Here below I have posted my JSON response and I wanted to get console output looks like below also. Please help me.
response : [ {
A = [ {
name : "sons";
age = [
4
];
},
{
name : "rondo";
age = [
2
];
},
];
} ]
I need to store separate array separate values looks like below console output
2014-09-18 10:24:39.461 Myapp[1133:60b] RESULT : {
name = "sons";
age = 4;
}
2014-09-18 10:24:39.462 Myapp[1133:60b] RESULT : {
name = "rondo";
age = 2;
}
Here below I tried but I Know I can get only 0th index value but I need to get all index value from JSON response array:
myvalue = [NSString stringWithFormat:#"%#",[[[[responsData objectAtIndex:0] valueForKey:#"A"] objectAtIndex:0] valueForKey:#"name"]];
if you want to get all objects, get the array using the respective key.Then store the result in another array by iterating the for loop according to array count.
NSArray *recordsArr = [[responsedata objectAtIndex:0] valueForKey:#"A"];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [recordsArr count]; i ++) {
NSMutableDictionary *recordDict = [[NSMutableDictionary alloc] init];
[recordDict setObject:[[recordsArr objectAtIndex:i] valueForKey:#"name"] forKey:#"name"];
[recordDict setObject:[[[recordsArr objectAtIndex:i] valueForKey:#"age"] objectAtIndex:0] forKey:#"age"];
[resultArray addObject:recordDict];
}
NSLog(#"%#",resultArray);
output:
2014-09-18 12:49:22.047 testprj[1044:60b] (
{
age = 4;
name = sons;
},
{
age = 2;
name = rondo;
}
)
NSString *strName = (NSString *) [yourArray valueForKey:#"name"];
NSInteger age = [(NSNumber *) [yourArray valueForKey:#"age"] integerValue];

Get the dictionary which is the latest in array iOS

I have array contains many dictionary as below:
Array = {
buyerID = 00000000000w;
time = 1404919942804;
id = 0000000000Aa;
},
{
buyerID = 00000000000Z;
time = 1404670971396;
id = 0000000000Aa;
},
{
buyerID = 00000000000Z;
time = 1406531476764;
id = 0000000000Ab;
},
{
buyerID = 00000000000w;
time = 1406531476213;
id = 0000000000Aa;
},
The expect result is:
LastArray = {
buyerID = 00000000000w;
time = 1404919942804;
id = 0000000000Aa;
},
{
buyerID = 00000000000Z;
time = 1404670971396;
id = 0000000000Aa;
},
{
buyerID = 00000000000Z;
time = 1406531476764;
id = 0000000000Ab;
},
That means I want to remove the last object because the BuyerID and ID are the same as the first item. How can i do that? Please give me some advice. Thanks so much.
If you want to remove duplicates try this -
NSMutableArray *objectsToRemove = [[NSMutableArray alloc]init];
for (int i=0; i<mutableArray.count; i++) {
for (int j=i+1; j<mutableArray.count; j++) {
if ([[[mutableArray objectAtIndex:i] valueForKey:#"buyerID"] isEqualToString:[[mutableArray objectAtIndex:j]valueForKey:#"buyerID"]] && [[[mutableArray objectAtIndex:i] valueForKey:#"id"] isEqualToString:[[mutableArray objectAtIndex:j]valueForKey:#"id"]]) {
[objectsToRemove addObject:[mutableArray objectAtIndex:j]];
}
}
}
[mutableArray removeObjectsInArray:objectsToRemove];
NSLog(#"AFTER :: %#",mutableArray);
Replace mutableArray by your array.
If the Array(data) is fixed, then simply removeAtObject is enough, [myArray removeObjectAtIndex:myArray.count-1];OR [myArray removeLastObject];
If you want to remove duplicates, then Check this
'containsObject' is your friend here. It can check for complex object types within an array. So as part of your code that is adding the objects to the mutable array, you can add a filter to stop duplicates...
NSMutableArray *cleanArray = [[NSMutableArray alloc] init];
if (![cleanArray containsObject:dictionaryToAdd]) {
[cleanArray addObject:dictionaryToAdd];
}
So concentrate at the place where the array is being populated by these objects and add the filter code above to ensure only unique objects are added. NSSet might help also.

Adding Multiple NSArrays to NSMutableDictionary with corresponding matching item in array order

I have two arrays say itemName and itemImages
NSArray *itemName = #[#"Blog", #"Missions", #"Subscriptions", #"Questions", #"Aide",#"Disconnect"];
NSArray *itemImages = #[#"ico-1",#"ico-2", #"ico-3", #"ico-4", #"ico-5", #"ico-6"];
I need to add them to a dictionary
NSMutableDictionary *menuItemNameImage = [[NSMutableDictionary alloc] init];
[menuItemNameImage setValue:itemName forKey:#"itemName"];
[menuItemNameImage setValue:itemImages forKey:#"itemImages"];
NSLog(#"%#", menuItemNameImage);
But this makes the following Dictionary
{
itemImages = (
"ico-1",
"ico-2",
"ico-3",
"ico-4",
"ico-5",
"ico-6"
);
itemName = (
Blog,
Missions,
Subscriptions,
Questions,
Aide,
Disconnect
);
}
What I rather want is some think like this :
{
{
itemImages = "icon-1",
itemName = Blog
},
{
itemImage = "icon-2",
itemName = "Missions"
}, ...
}
You cannot get that structure with a single NSDictionary - you need an NSArray of NSDictionary objects.
Here is how you can do it:
NSMutableArray *menuItemNameImage = [NSMutableArray array];
for (int i = 0 ; i != itemNames.count ; i++) {
[menuItemNameImage addObject: #{
#"itemName" : itemNames[i]
, #"itemImage" : itemImages[i]
}];
}
What you could use is a NSArray of NSDictionaries. Each dictionary would have two entries: an itemImage and itemName.
It would be something like this:
NSArray *itemName = #[#"Blog", #"Missions", #"Subscriptions", #"Questions", #"Aide",#"Disconnect"];
NSArray *itemImages = #[#"ico-1",#"ico-2", #"ico-3", #"ico-4", #"ico-5", #"ico-6"];
NSMutableArray *menuItemNameImage = [NSMutableArray new];
for (NSInteger i = 0; i < itemName.count; i++) {
NSMutableDictionary *itemNameImage = [NSMutableDictionary new];
itemNameImage[#"itemName"] = itemName[i];
//just a precaution if the array os images is smaller than the ones of names, to avoid unwanted crashes
if (i < itemImages.count) {
itemNameImage[#"itemImage"] = itemImages[i];
}
[menuItemNameImage addObject:itemNameImage];
}
NSLog(#"%#", menuItemNameImage);
The output, as you want:
(
{
itemImage = "ico-1";
itemName = Blog;
},
{
itemImage = "ico-2";
itemName = Missions;
},
{
itemImage = "ico-3";
itemName = Subscriptions;
},
{
itemImage = "ico-4";
itemName = Questions;
},
{
itemImage = "ico-5";
itemName = Aide;
},
{
itemImage = "ico-6";
itemName = Disconnect;
}
)
You can access the elements like this, for example:
NSString *firstItemName = menuItemNameImage[0][#"itemName"];
NSString *firstItemImage = menuItemNameImage[0][#"itemImage"];

Resources