|
使用iOS 8.0。6 P! ]* o8 F5 l7 k; P
我Google API用来获取位置。我想减少使用。NSPredicate和KeyPath的编码。
) ~4 x0 B7 G O1 e, s; }! s+ W如果我是Sql开发人员,我会写类似的查询,% z; T, X) O7 ~
1. (select address_components from MAIN_TABLE where types like 'postal_code') from this I will get NAME_TABLE rows.2.select long_name from NAME_TABLE where types like 'locality'为此,我正在使用下面的代码,看着 @ztan 的答案之后,我已经改变了这样的代码" J1 ]2 C" I' H0 k& u
编辑:答案% m( H. h2 i0 d E' t
NSArray *addressArray = json[@"results"];NSPredicate *predicateForTypes=[NSPredicate predicateWithFormat"(types CONTAINS 'postal_code')"];addressArray=([addressArray filteredArrayUsingPredicate:predicateForTypes][0])[@"address_components"];NSPredicate *predicateForCity=[NSPredicate predicateWithFormat"(types CONTAINS 'locality')"];NSString *cityName=([addressArray filteredArrayUsingPredicate:predicateForCity][0])[@"long_name"];我在JSON以下…
2 F0 ?6 Q6 q; q: s+ C "address_components" = ( "long_name" = "short_name" = types = "street_number" "long_name" = "Juniper Drive"; "short_name" = "Juniper Dr"; types = route "long_name" = "North Kingstown"; "short_name" = "North Kingstown"; types = locality, political ); }, "long_name" = "Washington County"; "short_name" = "Washington County"; types = "administrative_area_level_2", political ); "long_name" = "Rhode Island"; "short_name" = RI; types = "administrative_area_level_1", political ); "long_name" = "United States"; "short_name" = US; types = country, political ); "long_name" = "short_name" = types = ( "postal_code" ); "long_name" = "short_name" = types = ( "postal_code_suffix" ); ); "formatted_address" = "221 Juniper Drive,North Kingstown,RI 02852,USA"; geometry = location = lat = "41.577761"; lng = "-71.468383"; "location_type" = ROOFTOP; viewport = northeast = lat = "41.5791099802915"; lng = "-71.46703401970849"; southwest = lat = "41.5764120197085"; lng = "-71.4697319802915"; }; }; "place_id" = ChIJ563yzFex5YkRujoi28Fvzvo; types = ( "street_address" ) "address_components" = ( "long_name" = "North Kingstown"; "short_name" = "North Kingstown"; types = locality, political ); }, "long_name" = "Washington County"; "short_name" = "Washington County"; types = "administrative_area_level_2", political ); "long_name" = "Rhode Island"; "short_name" = RI; types = "administrative_area_level_1", political ); "long_name" = "United States"; "short_name" = US; types = country, political ); ); "formatted_address" = "North Kingstown,RI,USA"; geometry = bounds = northeast = lat = "41.6549521"; lng = "-71.4023083"; southwest = lat = "41.497126"; lng = "-71.52244109999999"; location = lat = "41.5568315"; lng = "-71.4536835"; "location_type" = APPROXIMATE; viewport = northeast = lat = "41.6549521"; lng = "-71.4023083"; southwest = lat = "41.497126"; lng = "-71.52244109999999"; }; }; "place_id" = "ChIJgWUP-V-x5YkRO7Zie7NXWsI"; types = ( locality, political )nd JSON Object,3rd JSON Object ,......,....我的问题是如何减少编码?NSPRedicates和KeyPath ..2 q5 v4 p+ ]5 ~
这就是我们如何解决问题的方法……我在@ztan答案中提到了这一点,请看一看,这将是有益的。
5 O- a7 C) A Y8 l; h. x请看我的第二条评论.......
: I0 T4 F$ P6 S$ y; }9 F$ L + j- i+ g$ x3 @: @: Q
解决方案: |
|