gpt4 book ai didi

objective-c - 键 IOS 值的字典检索

转载 作者:行者123 更新时间:2023-11-29 11:09:41 25 4
gpt4 key购买 nike

对不起,我是IOS新手,我想不出解决这个问题的方法

这只是初学者餐厅的菜单

有一个包含项目和价格的表格 View ,当我单击一个项目时,它会显示另一个 View ,用户必须在其中输入数量并单击完成按钮,因此当用户单击完成时,我想将数量乘以价格,我如何检索该特定价格并将其与用户在文本字段中输入的数量相乘。

这是我的代码

我已经在名为

的菜单头文件中声明了 NSDictionary
NSDictionary *dict;

我的viewdidload方法

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

我已经在表格 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];
}
}

蔬菜数量.h有一个 View 有一个文本字段和一个按钮说完成,现在,当我单击完成按钮时,我需要检索该特定汤的值并将其乘以我输入的数量。我的问题是我应该如何检索该特定键的价格(值)并将其乘以数量。

最佳答案

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

该方法是 initWithObjectsAndKeys,这意味着首先是对象,然后是键,(键“20.00”,对象 - “TomatoSoup”)- 在您的情况下恰恰相反。

其次,使用 NSNumber - [NSNumber numberWithFloat:20.0f] 而不是价格的 NSString(我想它是价格或数量)。

然后,制作你的 VegQuantity View Controller (顺便说一句,为了保持命名约定,将它称为 VegQuantityViewController 是个好主意)2 个属性:

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

并在显示之前将这些值传递给 View Controller 。然后在里面你可以用它们做任何你想做的事。 附言使用属性来操纵实例变量的值是一种很好的做法。

关于objective-c - 键 IOS 值的字典检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12104402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com