繁体   English   中英

如果从数组的B列中选择特定值,如何从A列中获取值?

[英]How can I get value from Column A if select specific value from Column B in an Array?

我有一个带有“ Country_ID”和“ Country_Name”列的“ Country”数组。 如果“ Country_ID”为123,如何获取“ Country_Name”值? 我可以在valueForKey中完成它吗?

我该怎么写? 示例国家/地区ID = 123,国家/地区名称=日本

NSArray *ary = [CountryArray valueForKey:@"Country_Name"];

您可以使用NSPredicate来过滤结果,例如:

NSString *countryID=@"123";
NSString *countryName=@"Japan";
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"Country_ID = %@ AND Country_Name = %@",countryID,countryName];
NSArray *filteredArray=[CountryArray filteredArrayUsingPredicate:predicate];

如果要将Country_IDCountry_Name关联,则可以使用字典。

NSArray *countryIDs = [NSArray arrayWithObjects: @"key", @"123", nil];
NSArray *countryNames = [NSArray arrayWithObjects: @"value", @"Japan", nil];

NSDictionary *dict = [[NSDictionary alloc] initWithObjects: countryNames forKeys: countryIDs];
NSLog(@"%@", [dict objectForKey: @"123"]);//prints out "Japan"

Apple提供了有关在Objective-C中使用字典的示例文档。

https://developer.apple.com/documentation/foundation/nsdictionary?changes=_4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM