ios 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSCFString objectForKey:]:无法识别的选择器发送到实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12119714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:52:19  来源:igfitidea点击:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance

objective-ciosnsdictionary

提问by BoredToDeath

I have the following code in the main header file

我在主要有以下代码 header file

NSMutableArray *array;
NSDictionary *dict,*dict1;

.mfile contains array of dictionaries which display the item name along with their resp prices, I have displayed this in UITableView

.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;        
   }

when the user selects any one item it sends the dictionaryanother dictionarycalled pass to VegQuantityViewControllerwhere quantity has to multiplied with the price

当用户选择任何一个项目时,它会将dictionary另一个dictionary调用的传递发送到VegQuantityViewController数量必须乘以价格的位置

- (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

VegQuantity.h

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

VegQuantity.m

VegQuantity.m

The donebutton just retrieves the key for that particular dish and multiplies it with the quantity user inputs in the textfieldand displays it in the output textfieldI am facing NSInvalidArgumentExceptionon pass objectForKeyline, what can be the problem?

done按钮只是检索该特定菜肴的密钥,并将其与用户在 中输入的数量相乘,并将其textfield显示在textfieldNSInvalidArgumentException在通过objectForKey线上面临的输出中,这可能是什么问题?

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

回答by Apurv

Change below code in viewDidLoad method.

在 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];

回答by Sj.

In your case array, dict and dict1 are autoreleasedobjects. Retain these member variable in the viewDidLoadmethod. Or have a retain property to these variables. It is crashing because the dict is being released. The crash says that the variable dict is of type CFString, which might be caused due to autoreleasingof that object.

在您的案例数组中, dict 和 dict1 是autoreleased对象。在viewDidLoad方法中保留这些成员变量。或者对这些变量具有保留属性。它正在崩溃,因为 dict 正在发布。崩溃表明变量 dict 的类型为CFString,这可能是autoreleasing由该对象引起的。