0

この応答をエレガントな方法で解析することはできません。さまざまなフィールドタイプを、次の属性を含むオブジェクトに一致させようとしています。

@property (nonatomic) NSString *name;
@property (nonatomic) NSString *address;
@property (nonatomic) NSString *city;
@property (nonatomic) NSString *postalcode;
@property (nonatomic) NSString *district;
@property (nonatomic) NSString *country;

過去に私は辞書を調べましたが、サブフィールドは検索されたフィールドと一致していました。

 {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "10",
               "short_name" : "10",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Warschauer Straße",
               "short_name" : "Warschauer Straße",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Friedrichshain",
               "short_name" : "Friedrichshain",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Friedrichshain-Kreuzberg",
               "short_name" : "Friedrichshain-Kreuzberg",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Germany",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "10243",
               "short_name" : "10243",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Warschauer Straße 10, 10243 Berlin, Germany",
         "geometry" : {
            "location" : {
               "lat" : 52.51329350,
               "lng" : 13.45246930
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 52.51464248029150,
                  "lng" : 13.45381828029150
               },
               "southwest" : {
                  "lat" : 52.51194451970850,
                  "lng" : 13.45112031970850
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
4

2 に答える 2

1

Ok。私は次の解決策を思いついた。辞書を定義し、セッターを上書きして、着信辞書を自分のパーサーに解析しました。

-(void)addressByGoogle: (NSDictionary*)newAddressComponents{

    for (NSArray *googleResult in newAddressComponents) {

        for (NSArray *fields in googleResult) {

            if ([[fields valueForKey:@"types"] count] >= 1) {

                NSString *googleType = [[fields valueForKey:@"types"] objectAtIndex:0];

                if ( [googleType isEqualToString:@"sublocality"]) {
                    self.district = [fields valueForKey:@"long_name"];
                }

                if ( [googleType isEqualToString:@"country"]) {
                    self.country = [fields valueForKey:@"long_name"];
                }

                if ( [googleType isEqualToString:@"locality"]) {
                    self.city = [fields valueForKey:@"long_name"];
                }

                if ( [googleType isEqualToString:@"street_number"]) {
                    self.streetnumber = [fields valueForKey:@"long_name"];
                }

                if ( [googleType isEqualToString:@"route"]) {
                    self.address = [fields valueForKey:@"long_name"];
                }

                if ( [googleType isEqualToString:@"postal_code"]) {
                    self.postalcode = [fields valueForKey:@"long_name"];
                }
                if ( [googleType isEqualToString:@"administrative_area_level_1"]) {
                    self.adminAreaLevel1 = [fields valueForKey:@"long_name"];
                }
                if ( [googleType isEqualToString:@"administrative_area_level_2"]) {
                    self.adminAreaLevel2 = [fields valueForKey:@"long_name"];
                }
            }

        }
    }

}

- (void) setAddressComponents:(NSDictionary *)newAddressComponents{
    [self addressByGoogle: newAddressComponents];
}
于 2013-02-26T15:59:16.287 に答える
0

ジムが言ったように、Restkitを使用してこのオブジェクトをマップするエレガントな方法はありません。代わりに、このフォーマットを「醜いオブジェクト」に直接マップし、そのオブジェクトを必要なインターフェイスを持つ別のオブジェクトでラップすることができます。

何かのようなもの:

@interface MYMappedAddress : NSObject

@property (nonatomic) NSArray *addressComponents;
@property (nonatomic) NSArray *types;
@property (nonatomic) MYGeometry *geometry;
@property (nonatomic) NSString * formattedAddress;

@end

@interface MYAddress : MYMappedAddress

@property (nonatomic) NSString *name;
@property (nonatomic) NSString *address;
@property (nonatomic) NSString *city;
@property (nonatomic) NSString *postalcode;
@property (nonatomic) NSString *district;
@property (nonatomic) NSString *country;

@end
于 2013-01-25T14:55:03.060 に答える