Iterating through a Dictionary - ios

I'm trying to iterate over an NSMutableDictionary and I cannot seem to get what I want. I have a dictionary mapping strings to colors like so...
squareColors = [NSMutableDictionary dictionaryWithObjects: [NSMutableArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
nil]
forKeys: [NSMutableArray arrayWithObjects:
#"yellow",
#"blue",
#"green",
#"purple",
#"orange",
nil]];
Over time the value of each entry will increase. Every once in a while I want to look into the dictionary and select the color with the highest count. How might I do that? Here's what I'm trying, but I'm unfamiliar with blocks.
__block int mostSquares = 0;
__block NSString* color = #"";
/* Look through the dictionary to find the color with the most number of squares */
[squareColors enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(#"%# => %#", key, obj);
NSInteger count = [key integerValue];
if (count > mostSquares)
{
color = key;
mostSquares = count;
}
}];

You have a very simple bug in your code. This line:
NSInteger count = [key integerValue];
should be:
NSInteger count = [obj integerValue];
'key' is the color name, obj is the number. As you have it, count gets set to 0 for each iteration because calling integerValue on a non-numeric string gives you 0.

Simple solution using your example:
NSMutableArray *arrayNumbers = [NSMutableArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:6],
[NSNumber numberWithInt:4],
nil];
NSMutableArray *arrayColours = [NSMutableArray arrayWithObjects:
#"yellow",
#"blue",
#"green",
#"purple",
#"orange",
nil];
NSMutableDictionary *squareColors = [NSMutableDictionary dictionaryWithObjects:arrayNumbers
forKeys:arrayColours];
NSUInteger indexOfArray = [arrayNumbers indexOfObject:[arrayNumbers valueForKeyPath:#"#max.intValue"]];
NSLog (#"Colour with largest value is %#", [arrayColours objectAtIndex:indexOfArray]);

Can you store your Keys into an array and iterate using that array? That would probably be the most efficient solution, since you'll need to know every key anyway.

Related

How to IntersectSet of two NSDictionary or NSMutableDictionary with values?

Want to merge two NSDictionary or NSMutableDictionary only with dict1 values. like [NSMutableDictionary addEntriesFromDictionary:] and intersectSet (from NSMutableSet).
Dict 1 = #{#”One”: #””, #”Two”: #”"}
Dict 2 = #{#”One”: #”1”, #”Two”: 2, #”Three”: #”3"}
My expect output after merge = #{#”One”: #”1”, #”Two”: #”2"}
UPDATE
I am already tried with [Dict1 addEntriesFromDictionary:Dict2] this one give answer #{#”One”: #”1”, #”Two”: 2, #”Three”: #”3"}
Also I am tried with NSMutableSet
NSMutableSet *keysInA = [NSMutableSet setWithArray:tempDict.allKeys];
NSSet *keysInB = [NSSet setWithArray:tempDict1.allKeys];
[keysInA intersectSet:keysInB];
NSLog(#"keys in A that are not in B: %#", keysInA);
This one give output One, Two, Three.
So [NSMutableSet setWithArray:] and [NSMutableDictionary addEntriesFromDictionary:] won't give what I expected.
If above code have anything work let clear me. Thanks.
NSDictionary *dict1 = #{#"One": #"", #"Two": #"6"};
NSDictionary *dict2 = #{#"One": #"1", #"Two": #"2", #"Three": #"3"};
NSDictionary * dict = [dict2 dictionaryWithValuesForKeys:[dict1 allKeys]];
NSLog(#"dict %#",dict);
// Log value
dict {
One = 1;
Two = 2;
}
Here what I expected.
try this...
//Assuming both dictionaries have same kind of keys
NSMutableDictionary *dict1 = #{#"One" : #"", #"Two": #"", #"Three" : #""}.mutableCopy;
NSDictionary *dict2 = #{#"One" : #"1", #"Two": #"2", #"Three" : #"3",#"Four":#"4"};
for (NSString *key in dict1.allKeys)
{
if (dict2[key])
{
dict1[key] = dict2[key];
}
}
NSLog(#"Updated dict %#",dict1);

sort array specified indexes IOS

I' want to filter NSARRAY based on an object named id, I've specific set of Ids that I want to filter and want to be first in NSARRAY.
I stored the following Ids as NSNUMEBR
NSNumber *A = [NSNumber numberWithInt:1122];
NSNumber *B = [NSNumber numberWithInt:1345];
NSNumber *C = [NSNumber numberWithInt:1667];
NSNumber *D = [NSNumber numberWithInt:1223];
NSNumber *E = [NSNumber numberWithInt:1213];
NSNumber *F = [NSNumber numberWithInt:1123];
NSNumber *G = [NSNumber numberWithInt:1555];
NSNumber *H = [NSNumber numberWithInt:1666];
NSNumber *I = [NSNumber numberWithInt:1567];
These are the set of ids that I want to filter and want to be first in my NSARRAY (Can be NSMutableArray for operation)
EDIT 1:
the NSARRAY is basically getting the id object as
Ids = [dict valueForKey:#"id"];
That selective ids are stored in NSNUMBER A to I
It is unclear what you are asking, as indicated by the down votes and comments. But let's see if we can help. I think the following pseudo-code algorithm is what you are asking for:
MutableArray frontItems, rearItems;
for every item in sourceArray
if item["id"] is in the collection of specific IDs
then add item to end of frontItems
else add item to end of rearItems
add rearItems to end of frontItems to give result
Write that in Objective-C and I think you have what you want.
HTH
//Creat array have all item : A->I
NSNumber *A = [NSNumber numberWithInt:1122];
NSNumber *B = [NSNumber numberWithInt:1345];
NSNumber *C = [NSNumber numberWithInt:1667];
NSNumber *D = [NSNumber numberWithInt:1223];
NSNumber *E = [NSNumber numberWithInt:1213];
NSNumber *F = [NSNumber numberWithInt:1123];
NSNumber *G = [NSNumber numberWithInt:1555];
NSNumber *H = [NSNumber numberWithInt:1666];
NSNumber *I = [NSNumber numberWithInt:1567];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:A,B,C,...,I, nil];
for (NSNumber *idx in arr) {
// To do
}

argument to 'NSDictionary' method 'dictionaryWithObjectsAndKeys:' should be an objective-C pointer type, not int

-(NSMutableArray*)getLSBGoalList
{
int userId = [[[AppManager sharedAppManager].userInfo objectForKey:USER_ID] intValue];
NSString *query = [NSString stringWithFormat:#"SELECT * FROM tblLBGoal WHERE userId = %d ORDER BY groupId", userId];
FMResultSet *rs = [database executeQuery:query];
NSMutableArray *arrLSBGoals = [NSMutableArray array];
while([rs next])
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:[rs intForColumn:#"lbGoalId"]], #"lbGoalId",
[NSNumber numberWithInt:[rs intForColumn:#"groupId"]], #"groupId",
[NSNumber numberWithInt:[rs intForColumn:#"userId"]], #"userId",
[rs stringForColumn:#"uuid"],#"uuid",
[NSNumber numberWithInt:[rs intForColumn:#"dataSyncType"]], #"dataSyncType",
[NSNumber numberWithInt:[rs intForColumn:#"idealValue"]], #"idealValue",
[NSNumber numberWithInt:[rs intForColumn:#"targetValue"]], #"targetValue",nil,
[NSNumber numberWithInt:[rs intForColumn:#"isTargetSet"]], #"isTargetSet",nil];
[arrLSBGoals addObject:dict];
}
return arrLSBGoals;
}
I am using the above code and when I analyse my project I got a warning, Argument to 'NSDictionary' method 'dictionaryWithObjectsAndKeys:' should be an objective-C pointer type, not int. Analyser show me the below way to fix it but I can't understand what to do. Can anybody please help me.
You have an extra nil in the line before last:
[NSNumber numberWithInt:[rs intForColumn:#"targetValue"]], #"targetValue",nil, // here
[NSNumber numberWithInt:[rs intForColumn:#"isTargetSet"]], #"isTargetSet",nil];
using the newer Objective-C dictionary literal syntax is much easier to read and doesn't require a terminating nil:
NSDictionary *dict = #{
#"lbGoalId" : #([rs intForColumn:#"lbGoalId"]),
#"groupId" : #([rs intForColumn:#"groupId"]),
// etc.
};
When creating dictionary you are trying to set nil in between. When I try to execute this I donot face any warning
-(NSMutableArray*)getLSBGoalList
{
// int userId = [[[AppManager sharedAppManager].userInfo objectForKey:USER_ID] intValue];
// NSString *query = [NSString stringWithFormat:#"SELECT * FROM tblLBGoal WHERE userId = %# ORDER BY groupId", #""];
MyClass *rs=self;
NSMutableArray *arrLSBGoals = [NSMutableArray array];
while([rs next])
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:[rs intForColumn:#"lbGoalId"]], #"lbGoalId",
[NSNumber numberWithInt:[rs intForColumn:#"groupId"]], #"groupId",
[NSNumber numberWithInt:[rs intForColumn:#"userId"]], #"userId",
[rs stringForColumn:#"uuid"],#"uuid",
[NSNumber numberWithInt:[rs intForColumn:#"dataSyncType"]], #"dataSyncType",
[NSNumber numberWithInt:[rs intForColumn:#"idealValue"]], #"idealValue",
[NSNumber numberWithInt:[rs intForColumn:#"targetValue"]], #"targetValue",
[NSNumber numberWithInt:[rs intForColumn:#"isTargetSet"]], #"isTargetSet",nil];
[arrLSBGoals addObject:dict];
}
return arrLSBGoals;
}
- (BOOL)next{
return YES;
}
- (int)intForColumn:(NSString *)str{
return 0;
}
- (NSString *)stringForColumn:(NSString *)str{
return #"";
}

convert array format according to array character

I have array in this format
rows = [[NSArray alloc] initWithObjects:#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica",
#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug" #"drums",
#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral",
#"Mark", #"Madeline",
#"Nemesis", #"nemo", #"name",
#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario",
#"Zeus", #"Zebra", #"zed", nil];
But i need this in to following format
rows = #[#[#"adam", #"alfred", #"ain", #"abdul", #"anastazja", #"angelica"],
#[#"dennis" , #"deamon", #"destiny", #"dragon", #"dry", #"debug", #"drums"],
#[#"Fredric", #"France", #"friends", #"family", #"fatish", #"funeral"],
#[#"Mark", #"Madeline"],
#[#"Nemesis", #"nemo", #"name"],
#[#"Obama", #"Oprah", #"Omen", #"OMG OMG OMG", #"O-Zone", #"Ontario"],
#[#"Zeus", #"Zebra", #"zed"]];
Means that same starting character in to different dictionary
The easiest approach.
NSArray *rows = ...;
NSMutableDictionary *map = [NSMutableDictionary dictionary];
for (NSString *value in rows) {
NSString *firstLetter = [value substringToIndex:1];
if (!map[firstLetter]) {
map[firstLetter] = #[];
}
NSMutableArray *values = [map[firstLetter] mutableCopy];
[values addObject:value];
map[firstLetter] = values;
}
NSArray *finalRows = [map allValues];
Note that finalRows is not sorted.
If you want to sort your array by it's first letter, you can try this :
NSMutableArray *outputArray = [NSMutableArray new];
NSString *lastFirstLetter = nil;
for(NSString *value in rows) {
NSString *firstLetter = [[value substringToIndex:1] lowerString];
if(![lastFirstLetter isEqualToString:firstLetter]) {
lastFirstLetter = firstLetter;
[outputArray addObject:[NSMutableArray new]];
}
[[outputArray lastObject] addObject:value];
}
The idea is to iterate your input array and if the first letter of your word is different than the precedent, create a new array.

Retrieve values from NSDictionary (which is stored in a NS Mutable Array) from another class

i've stored colors in a dictionary which is added to a nsmutable array.i want to retrieve values of the dictionary from another class based on the keys.thanks in advance
-(NSMutableArray *) GetAllColor
{
UIColor *aminocolor1 = [UIColor colorWithRed:.09 green:.56 blue:.30 alpha:.1];
UIColor *aminocolor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:.1];
NSDictionary *colordict = [NSDictionary dictionaryWithObjectsAndKeys:
aminocolor1, #"1",
aminocolor2, #"2",
nil];
NSMutableArray *colors = [NSMutableArray arrayWithObjects:colordict, nil];
return colors;
}
You can just chain messages:
NSDictionary* colorDict = [colors objectAtIndex:0];
UIColor *aminocolor = [colorDict objectForKey:#"1"];
Or the same in one line:
UIColor *aminocolor = [[colors objectAtIndex:0] objectForKey:#"1"];
Seeing this piece of code, I have to ask, why do you add the NSDictionary into an NSArray ? Why don't you just return the NSDictionary?
Now the simplest solution:
If you want to get all the NSDictionary from NSArray (if you have more then 1)
for(NSDictionary *dict in colors) {
//do whatever you want with the dictionary (now it's the dict that has the collors)
[dict objectForKey:#"1"] // this is how you get animcolor1.
}
If you want to get just a dictionary at a certain index:
if([colors count] > 0 && [colors count] < yourIndex) { //be smart and perform validations ;)
NSDictionary *dict = [colors objectAtIndex:yourIndex];
//do whatever you want with the dictionary (now it's the dict that has the collors)
[dict objectForKey:#"1"] // this is how you get animcolor1.
}
You can declare that array as a globally to use that array in some other class.

Resources