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];
}
}
}
Related
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.
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];
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.
This question already has answers here:
NSDictionary with ordered keys
(9 answers)
Closed 8 years ago.
I saw many examples on SO but I'm not sure if it applies to this situation. Everywhere it says NSMutableDictionries are not guaranteed an order...but I'm getting my data from the server...so my NSMuteDic looks like this:
{
joe = (
{
fromName = joe;
id = 25;
theMessage = "this is going to be a really big message...";
timeAdded = "2014-04-07 21:08:12";
toName = "me";
},
{
fromName = joe;
id = 10;
theMessage = "why???";
timeAdded = "2014-04-05 20:10:04";
toName = "me";
}
);
bob = (
{
fromName = "me";
id = 24;
theMessage = "blah blah";
timeAdded = "2014-04-06 21:15:06";
toName = bob;
},
{
fromName = bob;
id = 22;
theMessage = message;
timeAdded = "2014-04-06 20:11:57";
toName = "me";
}
);
//more entries here
}
What I want to do is change the order...put bob first and joe second. Is this really impossible to do? I saw many very complex solutions...there's no easy way to do this with just a for loop?
cellForRowAtIndexPath:
NSMutableArray *temp = [myDict objectForKey:keys[indexPath.row]];
cell.Message.text = [[reverse lastObject] valueForKey:#"theMessage"];
cell.dateTime.text = [[reverse lastObject] valueForKey:#"timeAdded"];
return cell;
This is how I'm using it...and when a row is selected I pass the array to the next view controller. The reason why I want to reorder is if a new message will be inserted in the pushed view controller, I want that dictionary to be first in the list so the root view controller can be reordered.
NSArray *keys = [myDict allKeys];
[myDict removeObjectForKey:to];
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:[myDict.count];
temp = myDict;
[myDict removeAllObjects];
[myDict setObject:currentDict forKey:to];
for (int i=0; i<temp.count; i++) {
[myDict setObject:[temp valueForKey:keys[i]] forKey:keys[i]];
}
That's not working because it looks like since myDict is a NSObject, temp gets changed every time myDict changes...from the looks of it the logic should work but it isn't...
NSMutableDictionary is not ordered. There is nothing you can do to guarantee the order of keys because NSDictionary makes no attempt to preserve any particular ordering. To go over a dictionary in a particular order, you have to make an array of the keys that is in the order you want, then iterate that and fetch the corresponding values.
// Get the keys
NSArray *keys = [myDict allKeys];
// Sort the keys
NSArray *sortedArray = [NSArray arrayWithArray:[keys sortedArrayUsingComparator:^(NSString* a, NSString* b) {
return [a compare:b];
}]];
// Iterate the dictionary
for (NSUInteger n = 0 ; < [sortedArray count]; n++) {
id value = [myDict objectForKey:[sortedArray objectAtIndex:n]];
}
I have a NSMutableArray "coordinates" which gives me values like this
2014-01-11 09:52:15.479 DreamCloud[397:70b] (
{
1000 = {
Linecolor = 0X6495ED;
Lines = "4-s";
Xcord = "77.000000";
Xposition = "54.500000";
Ycord = "111.500000";
Yposition = "51.500000";
};
},
{
1001 = {
Linecolor = 0X6495ED;
Lines = "4-s";
Xcord = "45.000000";
Xposition = "42.500000";
Ycord = "417.000000";
Yposition = "54.000000";
};
},
{
1000 = {
Linecolor = 0X6495ED;
Lines = "4-s";
Xcord = "73.000000";
Xposition = "50.500000";
Ycord = "111.000000";
Yposition = "51.000000";
};
}
)
Within this I need ti remove the values for 1001 and recreate the array without 1001 . How do I do this.I am new in ios so I did not figure out how to do this .
for (NSMutableDictionary *deltag in deletelinearray)
{
NSMutableDictionary *gettagsdeleted = [deltag objectForKey:[NSString stringWithFormat:#"%d",myV.tag]];
NSLog(#"%#",gettagsdeleted);
int starttag=[gettagsdeleted objectForKey:#"Starttag"];
int endtag=[gettagsdeleted objectForKey:#"Endtag"];
}
NSLog(#"%#",coordinates);
Above is the code where in "coordinates " I get the array and start-tags and end-tags are 1000, 1001 . In coordinates I don't know the index as Kumar Kl said .
Consider your array name is coorditates
for (NSDictionary *dict in coorditates) {
if ([dict objectForKey:[NSString stringWithFormat:#"%d",1001]]) {
[coorditates removeObject:dict];
break;
}
}
You can also use NSPredicate but, for that coorditates array must be NSMutableArray
NSPredicate *pred = [NSPredicate predicateWithFormat:#"ANY self.#allKeys != %#", #"1001"];
[coorditates filterUsingPredicate:pred];
Try this for loop instead. Instead of selecting the object with the key it removes it.
for (NSMutableDictionary *deltag in deletelinearray)
{
[coordinates removeObjectForKey:[NSString stringWithFormat:#"%d",myV.tag]];
}
It is not a good coding practice to modify(insert or delete) the items in array while enumerating it. So first find your index and then delete it.
You don't need to create a new array, once the details with "1001" is removed. You already have an update array.
NSString *deleteKey=#"1001";
__block int deleteIndex=-1;
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
for (NSString *key in [obj allKeys]) {
if([key isEqualToString:deleteKey]){
deleteIndex=idx;
*stop=YES;
}
}
}];
if(deleteIndex>=0){
[arr removeObjectAtIndex:deleteIndex];
}
You want to call the removeObjectAtIndex: function, but it appears your array contains dictionary objects and you want to remove the one with the key 1001. What you want to avoid is removing the object WHILE looping through the array.
So, what I would do is create a variable that will hold the index of the item once you find it, then loop through the array and once you find the object that it matches store the index and break. After, you shouldn't need to recreate the array after you remove the item, removing the object at the index you provide the above function will do just that for you. In your case, you'll want to do the following:
NSString *key = #"1001";
for (NSInteger index = 0; index < array.count; index++) {
NSDictionary *dictionary = coordinates[index];
if (dictionary[key]) {
indexToDelete = index;
break;
}
}
[coordinates removeObjectAtIndex:1001];
int i = 0;
for (NSDictionary *dict in coorditates)
{
if ([dict objectForKey:#"1001"])
{
[coorditates removeObjectAtIndex:i];
}
i++;
}