ipack26
ipack26

Reputation: 67

Dictionary retrieval of value for key IOS

I am sorry i am new to IOS,i couldnt figure out the solution to this problem

This is just a beginner restaurant menu

There is a tableview containing items and price and when i click one item,it displays another view where the user has to input the quantity and click a done button,so when the user clicks done i want to multiply the quantity times the price,how do i retrieve that particular price and multiply it with the quantity user input in the textfield.

Here's my code

I have declared NSDictionary in the Menu header file called

NSDictionary *dict;

My viewdidload method

dict=[[NSDictionaryalloc]initWithObjectsAndKeys:
@"TomatoSoup",@"20.00",@"VegManchowSoup",@"12.00",nil];
NSLog(@"%@",dict);
[super viewDidLoad];

I have displayed this contents in a table view

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
return [[dict allKeys]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] autorelease];
}

NSArray *sortedkeys=[[dict allKeys]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *key=[sortedkeys objectAtIndex:indexPath.row];
NSString *value=[dict objectForKey:key];
cell.textLabel.text=value;
cell.detailTextLabel.text=key;
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.row==0){   

VegQuantity *vegetarian1 = [[VegQuantity alloc]   initWithNibName:@"VegQuantity" bundle:nil];
vegetarian1.m_SelectedIndexPath=indexPath.row;
vegetarian1.pass=dict;
[self presentModalViewController:vegetarian1 animated:YES];
}
if(indexPath.row==1){   

VegQuantity *vegetarian1 = [[VegQuantity alloc] initWithNibName:@"VegQuantity" bundle:nil];
vegetarian1.m_SelectedIndexPath=indexPath.row;
[self presentModalViewController:vegetarian1 animated:YES];
}
}

VegQuantity.h There is a View that has a textfield and a button saying done, now when I click the done button I need to retrieve the value for that particular soup and multiply it with the number of quantity I input. My problem is how am I supposed to retrieve the price(value) for that particular key and multiply it with the quantity.

Upvotes: 1

Views: 4144

Answers (2)

graver
graver

Reputation: 15213

dict=[[NSDictionary alloc]initWithObjectsAndKeys:
                     @"TomatoSoup",@"20.00",@"VegManchowSoup",@"12.00",nil];

The method is initWithObjectsAndKeys, which means first is the object then key, (key "20.00", object - "TomatoSoup") - in your case it's the opposite.

Second, instead of having an NSString for the price (I suppose it's price or quantity) use NSNumber - [NSNumber numberWithFloat:20.0f].

Then, make your VegQuantity view controller (btw it's good idea to call it VegQuantityViewController, in order to keep the naming conventions) 2 properties:

@property (nonatomic, strong) NSString *itemName; //Use strong if using ARC,  otherwise retain
@property (nonatomic, strong) NSNumber *price;

and pass those values to the view controller before you show it. Then inside it you can do whatever you want with them. P.S. It's a good practice to use properties to manipulate the values of the instance variables.

Upvotes: 2

Maverick1st
Maverick1st

Reputation: 3770

You retrieve a value from a Dictionary by using.

[dict objectForKey:@"someDummyKey"];

But to be honest. You should use a NSMutableArray as datasource for you UITableView and not a NSDictionary.

Upvotes: 0

Related Questions