Ios6 如何在UIAlertView中使用NSMutableDictionary?

Ios6 如何在UIAlertView中使用NSMutableDictionary?,ios6,uialertview,nsmutabledictionary,Ios6,Uialertview,Nsmutabledictionary,我正在做一个卡路里计数器,我已经创建了一个UIAlertView,它给了我食物列表。我制作了一本NSMutableDictionary包含食物和卡路里: @implementation FoodDatabase -(id)init { self = [super init]; if(self) { food= [[NSMutableDictionary alloc] init]; [food setObject:@"111" forKey:@"Rice"

我正在做一个卡路里计数器,我已经创建了一个UIAlertView,它给了我食物列表。我制作了一本
NSMutableDictionary
包含食物和卡路里:

@implementation FoodDatabase

-(id)init 
{    
  self = [super init];
  if(self) 
  {
    food= [[NSMutableDictionary alloc] init];
    [food setObject:@"111" forKey:@"Rice"];       
  }
  return self;
}


-(NSString *) foodList: (NSString *) foodItem 
{
    for(NSString *key in [food allKeys]) 
    {        
        if([foodItem isEqual: key]) 
        {
            NSLog(@"%@",foodItem);
            return [food objectForKey:foodItem];
        }
    }
 }


@end
在另一个类中,我创建了一个
UIAlertView
,它提供了一个食物列表。这是项目Rice的代码段:

NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:@"Rice"])
{        
    NSString *xyz = [foodData foodList: @"Rice"];
    food_calorie = ([xyz floatValue]);

    UIAlertView *rice_alert=[[UIAlertView alloc] initWithTitle:@"Enter quantity of rice consumed" message:@"100 gms = 111 calories" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

    [rice_alert addTextFieldWithValue:@"" label:@"Enter quantity in gms"];

    RiceText = [rice_alert textFieldAtIndex:0];
    RiceText.keyboardType = UIKeyboardTypeNumberPad;
    RiceText.clearsOnBeginEditing = YES;
    RiceText.clearButtonMode = UITextFieldViewModeWhileEditing;
    RiceText.keyboardAppearance = UIKeyboardAppearanceAlert;
    rice_alert.tag = RiceAlertView;

    [rice_alert show];
    [rice_alert release];

}

我使用用户在
\u RiceText\u
中输入的值和为特定键(在本例中为大米)返回的
\u object\u
的值来计算总热量。但是它似乎没有返回
\u对象的值,因为
NSLog
显示
\u对象的值为
\u xyz
。我哪里做错了

整个错误在于对象
foodData
未正确初始化。所以,初始化它

很好的文档-

&从
NSmutableDictionary

:

请记住,在字典中用于比较的
键应具有与前面定义的相同的大小写(大写-小写)。e、 g存储键“example”的对象,但不能使用“example”或“example”检索该对象。您只能使用前面定义的“示例”。及


您不需要从字典中获取密钥数组&快速枚举它


您可以使用
objectForKey:
方法获取对象。祝你好运。

是否继承了
UIViewController
?@AkshitZaveri,我不确定我是否正确回答了你的问题,但这里是:UIViewController是主视图。。FoodDatabase是数据模式视图。。因此,我认为UIViewController继承了FoodDatabase当您尝试在
foodList:
NSString*xyz=[FoodDataFoodList:@“Rice”]中访问它时,您是否检查过ur
NSMutableDictionary
不是
nil
这部分代码应该检查NSMutableDictionary,但是从日志中我可以看到它是(null)检查
food=[[NSMutableDictionary alloc]init]是否执行?我认为它永远不会被执行。因此,您将得到
null
。在那里设置一个断点。完美答案。。一切都为我安排好了。。非常感谢。很高兴这有帮助谢谢@安尼什