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
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance
提问by BoredToDeath
I have the following code in the main header file
我在主要有以下代码 header file
NSMutableArray *array;
NSDictionary *dict,*dict1;
.m
file 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 dictionary
another dictionary
called pass to VegQuantityViewController
where 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 done
button just retrieves the key for that particular dish and multiplies it with the quantity user inputs in the textfield
and displays it in the output textfield
I am facing NSInvalidArgumentException
on pass objectForKey
line, what can be the problem?
该done
按钮只是检索该特定菜肴的密钥,并将其与用户在 中输入的数量相乘,并将其textfield
显示在textfield
我NSInvalidArgumentException
在通过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 autoreleased
objects. Retain these member variable in the viewDidLoad
method. 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 autoreleasing
of that object.
在您的案例数组中, dict 和 dict1 是autoreleased
对象。在viewDidLoad
方法中保留这些成员变量。或者对这些变量具有保留属性。它正在崩溃,因为 dict 正在发布。崩溃表明变量 dict 的类型为CFString
,这可能是autoreleasing
由该对象引起的。