2011-11-01 87 views
0

我已经创建了一个按钮操作方法。使用数组中的值初始化NSDictionary并为其分配值。在控制台中打印字典对象时可以获得正确的值。虽然分配给“的NSString *价值”的印刷值一样值为:144964992.但是在控制台上得到正确的价值观如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]); 

     } 
    } 

如果任何人能请提供的解决方案它会有所帮助......在此先感谢...

回答

1

的原因是,当你在gdb你实际上是打印的NSNumber的值打印这似乎是正确的控制台。但是你不能直接在NSString中使用NSNumber。所以,改线

NSString *value = [dict objectForKey:[choices objectAtIndex:j]]; 

  NSString *value = [NSString stringWithFormat: @"%d",[[dict objectForKey:[choices objectAtIndex:j]] integerValue]]; 
0

您有一个NSNumber对象字典,但将这些NSNumber对象之一分配给NSString,即使它们是不同类型的对象。打印操作试图将NSNumber作为NSString打印出来,从而产生不可预知的结果。

您可以使用

NSString *value = [[[dict objectForKey:[choices objectAtIndex:j]] stringValue]; 

得到适当的值转换成一个NSString。

+0

谢谢回复... – Susha

1

您正在试图TI打印使用"%d".%d一个字符串值整数values.the正确格式

NSLog(@"--------> Value is: %@", value); 
+0

它实际上是一个'NSNumber',而不是一个字符串。 – 2011-11-01 06:27:12