本文介绍了使用valueForKeyPath获取数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法访问 NSArray 元素与 valueForKeyPath ?例如,Google的反向地理编码器服务返回非常复杂的数据结构。如果我想得到城市,现在我必须把它分成两个调用,像这样:

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-like配方。

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获取数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:35