我在主header file
NSMutableArray *array;
NSDictionary *dict,*dict1;
.m
文件包含显示项目名称及其resp价格的字典数组,我已在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
另一个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
NSDictionary *pass;
IBOutlet UIButton *done;
IBOutlet UITextField *input,*output;
NSNumber *prices;
float x,y,c;
VegQuantity.m
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];
}
答案 0 :(得分:4)
在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];
答案 1 :(得分:2)
在你的case数组中,dict和dict1是autoreleased
个对象。在viewDidLoad
方法中保留这些成员变量。或者对这些变量保留属性。它正在崩溃,因为dict正在被释放。崩溃说变量dict的类型为CFString
,这可能是由于该对象的autoreleasing
引起的。