簡體   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