Reputation: 431
I have the following code in the main 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
- (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
- (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
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?
-(IBAction)done:(id)sender
{
prices=[pass objectForKey:@"price"];
x=[input.text floatValue];
y=[prices floatValue];
c=x*y;
output.text=[NSString stringWithFormat:@"%f",c];
}
Upvotes: 2
Views: 23116
Reputation: 1724
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.
Upvotes: 2
Reputation: 17186
Change below code in viewDidLoad method.
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];
Upvotes: 4