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
我在passobjectForKey
行中遇到了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,这将解决大多数此类问题。