I have created a button action method. Initialized an NSDictionary with values from an array and assigned values to it. While printing the objects of dictionary in console getting correct value. While assigning to “NSString *value” printing values like Value is: 144964992. But on console getting correct values like 0, 1,2, 3….
-(IBAction)takeDecision:(id)sender
{
NSLog(@"FINAL DECISION");
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
for(int i = 0; i < [choices count]; i++)
{
//[resultChoice indexOfObject:[NSNumber numberWithInt:i]];
[dict setObject:[NSNumber numberWithInt:i] forKey:[choices objectAtIndex:i]];
}
for (id key in dict) {
NSLog(@"key: %@, value: %@", key, [dict objectForKey:key]);
}
for(int i = 0; i < [preferences count]; i++)
{
for(int j = 0; j < [choices count]; j++)
{
//NSLog(@"Number of cells: %d", [[myTable] numberOfRowsInSection:0]); // This writes out "Number of cells: 6"
UITableViewCell *cell = [myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i]];
if(cell == nil) {
NSLog(@"CELL IS NIL");
}
NSArray *array = cell.contentView.subviews;
NSLog(@"NUMBER OF OBJECTS: %d",[array count]);
UIButton *test = (UIButton *)[array objectAtIndex:1];
UIImage *newImage = [test currentBackgroundImage];
NSLog(@"Image is %@",newImage.CGImage);
NSLog(@"*************TEST VALUE IS:%@",[dict objectForKey:[choices objectAtIndex:j]]);
NSString *value = [dict objectForKey:[choices objectAtIndex:j]];
NSLog(@"--------> Value is: %d", value);
if(newImage == [UIImage imageNamed:@"thumbsup_selected.png"])
{
[dict setValue:[NSNumber numberWithInt: value+1] forKey:[choices objectAtIndex:j]];
}
NSLog(@"CELL IS NOT NIL");
}
}
for (id key in dict) {
NSLog(@"key: %@, value: %@", key, [dict objectForKey:key]);
}
}
If any one can please provide a solution for it it will be helpful… Thanks in advance…
The reason is when you print in gdb you are actually printing value of NSNumber which seems correct in console. But you can not directly take NSNumber in NSString. So change the line
to