sort array specified indexes IOS - 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
}

Related

Increment a value in an int NSMutableArray

I've an NSMutableArray as
self.valuesArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt: 0],[NSNumber numberWithInt: 0],[NSNumber numberWithInt: 0],[NSNumber numberWithInt: 0], nil];
As you can see it's initialized with 0 now If I want to increment 3 at index 2 how can I do it? I was trying to do it like
self.valuesArray[2] = [self.valuesArray objectAtIndex:2]+3;
but that's not the right way.
I've also tried doing this
[self.valuesArray[2] addObject:[NSNumber numberWithInt:[self.valuesArray[2] intValue]+3]];
but I got error that:
Unrecognized selector sent to instance
You should do like this it will work.
int valueInc = [[self.valuesArray objectAtIndex:2] intValue] + 2;
NSNumber *numberValue = [NSNumber numberWithInt:valueInc];
[self.valuesArray replaceObjectAtIndex:2 withObject:numberValue];

How We store Float value in NSMutableDictionary

- (void)buttonSave:(UIButton *)bttnSave
{
UILabel *lbl = (UILabel *)[self.view viewWithTag:kTagLblText];
[dicStore setValue:lbl.textColor forKey:#"Text Color"];
// fltValue is float type
[dicStore setObject:fltValue forKey:#"font"];
[dicStore setValue:strtextView forKey:#"Text Style"];
[arrStoreDic addObject:dicStore];
}
If We can store this then any one can help me please do
Simply:
dicStore[#"font"] = #(fltValue);
However it's troubling that dicStore is an instance variable that you want to store into an array, which leads me to believe you will end up with an array of the same dictionary, containing the same values.
Instead create a new dictionary each time and store it into the array (unless, of course, other code needs to see this dictionary). The current implementation is very brittle.
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:0.7], [NSNumber numberWithFloat:0.2], nil],
nil];
The above code is used to add float values in Dictionary
NSMutableDictionary, NSarray, NSSet and other collection classes accept only objects, and a float variable is a primitive. You have to create a NSNumber object from your float :
NSNumber * floatObject =[NSNumber numberWithFloat:fltValue]
[dicStore setObject:floatObject forKey:#"font"];
Using literals, your code can be simplified to :
-(void)buttonSave:(UIButton *)bttnSave
{
UILabel *lbl = (UILabel *)[self.view viewWithTag:kTagLblText];
dicStore[#"Text Color"] = lbl.textColor;
dicStore[#"font"] = #(fltValue);
dicStore[#"Text Style"] = strtextView;
[arrStoreDic addObject:dicStore];
}
To get back the float value, it's pretty simple :
NSNumber * floatNumber = dicStore[#"font"];
float floatValue = floatNumber.floatValue;
NSDictionary *dict = #{#"floatProperty":[NSNumber numberWithFloat:floatValue]};

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 #"";
}

Add NSNumber 0 to NSDictionary

I need to create a dictionary with for a search, based on IDs, but if no ID gets selected, 0 has to be stored.
Now I can obviously do it like this:
NSNumber* cityID;
NSNumber* zoneID; //These two do get value, just showing for their class
NSDictionary *cityDictionary = #{#"CityID": [NSNumber numberWithInt:(int)cityId], #"CityZoneID": [NSNumber numberWithInt:(int)zoneId]};
I feel it's really messy like that. It gets cast to int, then turned back to NSNumber...
Try this
// May be syntax change but you need to initialise with zero first
NSNumber* cityID = [NSNumber numberWithInt:0];
NSNumber* zoneID = [NSNumber numberWithInt:0]; //Init with Zero then you can added value for this if no value default will be 0
NSDictionary *cityDictionary = #{#"CityID": [NSNumber numberWithInt:(int)cityId], #"CityZoneID": [NSNumber numberWithInt:(int)zoneId]};
This may help
What about
NSDictionary *cityDictionary = #{
#"CityID" : (cityID == nil ? #0 : cityID),
#"CityZoneID" : (zoneID == nil ? #0 : zoneID)
};
NSNumber* cityID;
NSNumber* zoneID;
if((cityID == nil)||(zoneID == nil))
{
NSDictionary *cityDictionary = #{#"CityID": [NSNumber numberWithInt:0], #"CityZoneID": [NSNumber numberWithInt:0]};
}

Iterating through a Dictionary

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.

Resources