使用 valueForKeyPath 获取数组元素 [英] Getting array elements with valueForKeyPath
问题描述
有什么方法可以使用 valueForKeyPath
访问 NSArray
元素?例如,谷歌的反向地理编码服务返回一个非常复杂的数据结构.如果我想得到这个城市,现在我必须把它分成两个调用,就像这样:
Is there any way to access an NSArray
element with valueForKeyPath
? Google's reverse geocoder service, for example, returns a very complex data structure. If I want to get the city, right now I have to break it into two calls, like this:
NSDictionary *address = [NSString stringWithString:[[[dictionary objectForKey:@"Placemark"] objectAtIndex:0] objectForKey:@"address"]];
NSLog(@"%@", [address valueForKeyPath:@"AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName"]);
只是想知道是否有办法将 objectAtIndex:
调用硬塞到 valueForKeyPath
字符串中.我尝试了像 @"Placemark[0].address" 这样的 javascript-esque 公式,但没有骰子.
Just wondering if there's a way to shoehorn the objectAtIndex:
call into the valueForKeyPath
string. I tried a javascript-esque formulation like @"Placemark[0].address" but no dice.
推荐答案
很遗憾,没有.允许使用键值编码的完整文档是此处一>.据我所知,没有任何运算符可以让您获取特定数组或设置对象.
Unfortunately, no. The full documentation for what's allowed using Key-Value Coding is here. There are not, to my knowledge, any operators that allow you to grab a particular array or set object.
这篇关于使用 valueForKeyPath 获取数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!