objective-c - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因 : '-[NSCFString objectForKey:]: unrecognized selector sent to instance

标签 objective-c ios nsdictionary

我在主头文件中有如下代码

NSMutableArray *array;
NSDictionary *dict,*dict1;

.m 文件包含字典数组,显示商品名称及其相应价格,我在 UITableView

中显示了它
 - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
   {
      return [array count];
   }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   { 
      static NSString *CellIdentifier = @"Cell";

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) 
        {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
      cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",[[array objectAtIndex:indexPath.row]objectForKey:@"name"],[[array objectAtIndex:indexPath.row]objectForKey:@"price"]];
                    return cell;        
   }

当用户选择任何一项时,它会发送 dictionary 另一个名为 pass 的 dictionaryVegQuantityViewController,其中数量必须乘以价格

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0)
    {           
         VegQuantity *vegetarian1 = [[VegQuantity alloc] initWithNibName:@"VegQuantity" bundle:nil];    
        vegetarian1.pass=dict;
        [self presentModalViewController:vegetarian1 animated:YES];
    }
    if(indexPath.row==1)
    {           
        VegQuantity *vegetarian1 = [[VegQuantity alloc] initWithNibName:@"VegQuantity" bundle:nil];
        vegetarian1.pass=dict1;
        [self presentModalViewController:vegetarian1 animated:YES];
     }
}

- (void)viewDidLoad 
{
    array=[NSMutableArray array];
    dict=[NSDictionary dictionaryWithObjectsAndKeys:@"20.00",@"price",@"tomato soup",@"name",nil];
    [array addObject:dict]; 
    dict1=[NSDictionary dictionaryWithObjectsAndKeys:@"10.00",@"price",@"Veg Manchow soup",@"name",nil];
    [array addObject:dict1];
    NSLog(@"The array of dictionaries contains%@",array);
    [super viewDidLoad];
}

VegQuantity.h

NSDictionary *pass;
IBOutlet UIButton *done;
IBOutlet UITextField *input,*output;
NSNumber *prices;
float x,y,c;

VegQuantity.m

done 按钮只是检索该特定菜肴的键,并将其与用户在 textfield 中输入的数量相乘,并将其显示在输出 textfield 我在传递 objectForKey 行时遇到 NSInvalidArgumentException,可能是什么问题?

-(IBAction)done:(id)sender
{   
    prices=[pass objectForKey:@"price"];
    x=[input.text floatValue];
    y=[prices floatValue];
    c=x*y;
    output.text=[NSString stringWithFormat:@"%f",c];
}

最佳答案

在 viewDidLoad 方法中更改以下代码。

array=[[NSMutableArray alloc]init];
dict=[[NSDictionary alloc] initWithObjectsAndKeys:@"20.00",@"price",@"tomato soup",@"name",nil];
[array addObject:dict]; 
dict1=[[NSDictionary alloc]initWithObjectsAndKeys:@"10.00",@"price",@"Veg Manchow soup",@"name",nil];
[array addObject:dict1];

关于objective-c - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因 : '-[NSCFString objectForKey:]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12119714/

相关文章:

ios - 字符串编码和表情符号问题

objective-c - 使用 ASIHTTPRequest 作为 NSDictionary Key

javascript - 用于搜索产品的 Ebay.com 查询字符串是什么?

ios - 如何显示 View ?

iphone - 为脉动的 UILabel 制作动画?

ios - swift 将 JSON 数据显示到 socket

ios - Objective-C - 使用相同值的两个字典,强值或弱值

ios - 位置管理器在iOS中的伊朗中返回空值

ios - 在 UIScrollview 中水平显示 pdf 文件