Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 由于未捕获异常而终止应用程序';NSInvalidArgumentException';,原因:'-[NSCFString objectForKey:]:发送到实例的选择器无法识别_Objective C_Ios_Nsdictionary - Fatal编程技术网

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

Objective c 由于未捕获异常而终止应用程序';NSInvalidArgumentException';,原因:'-[NSCFString objectForKey:]:发送到实例的选择器无法识别,objective-c,ios,nsdictionary,Objective C,Ios,Nsdictionary,我在主头文件中有以下代码 NSMutableArray *array; NSDictionary *dict,*dict1; .m文件包含一系列字典,这些字典显示商品名称及其对应价格,我已在UITableView - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { return [array count]; } - (UITableViewCe

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

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;        
   }
当用户选择任何一个项目时,它将另一个名为pass的
字典发送到
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

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

done
按钮仅检索特定盘子的钥匙,并将其与
textfield
中用户输入的数量相乘,并将其显示在输出
textfield
我在pass
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];
}

在case数组中,dict和dict1是
自动释放的
对象。在
viewDidLoad
方法中保留这些成员变量。或者对这些变量具有retain属性。它正在崩溃,因为dict正在被释放。崩溃表明变量dict的类型为
CFString
,这可能是由于该对象的
自动删除所导致的。

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

如果您将代码更改为ARC,这将解决大多数此类问题。