iOS - 从plist获取特定值

时间:2014-03-15 15:37:59

标签: ios plist

我想把NO(从plist下面)的值变成我得到的NSString。但是我被卡住了。

我使用了以下代码:

//Load Dictionary with wood name cross refference values for image name
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"Numbers" ofType:@"plist"];
NSDictionary *numberDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistCatPath];
self.numberArray = numberDictionary[@"Two"];

// ,[codeForCountryDictionary objectForKey:selectedCountryPicker]
number = [self.policeArray valueForKey:@"NO"];

NSLog(@"Numero: %@", number);

但后来我得到了:

(
012
)

我只想要012,而不是括号。

我的Plist: enter image description here

1 个答案:

答案 0 :(得分:0)

“NO”是“Two”NSDictionary中的NSString键,而NSString“012”是值,对于键“NO”

NSDictionary * dict = numberDictionary[@"Two"];
for (NSString * key in dict) {
  NSLog(@"%@ %@", key, dict[key]);
}

如果要从NSDictionary获取值的密钥,请使用

NSDictionary * dict = numberDictionary[@"Two"];
NSArray *temp = [dict allKeysForObject:@"012"];
NSString *key = [temp lastObject];

注意:allKeysForObject返回NSArray,因为更多的键可能具有相同的值