0

ZipCodeと呼ばれる次のクラスと、サードパーティのサービスからデータを正常に取得し、ローカルでインスタンス化されたZipCodeクラスにデータを入力するloadDataメソッドがあります。私が抱えている問題は、このクラスに入力された値を5つのローカル変数に変換し、入力された郵便番号を囲むストアをデータベースからプルバックするストアドプロシージャに渡すことができることです。

ZipCode.h

@interface ZipCodes : NSObject

@property (strong,nonatomic) NSString *city; //adminName2
@property (strong,nonatomic) NSString *stateAbbreviation; //adminCode1
@property (strong,nonatomic) NSString *postalCode; // postalCode
@property (strong,nonatomic) NSString *distance; //distance
@property (strong,nonatomic) NSString *country; // countryCode
@property (strong,nonatomic) NSString *stateName; //adminName1

-(id)initWithName:(NSString *)city stateAbbrev:(NSString *)stateAbbreviation postalCode:(NSString *)postalCode distanceFromGPSZip:(NSString *)distance country:(NSString *)country stateFullName:(NSString *)stateName;

@end

ZipCode.m

@implementation ZipCodes

-(id)initWithName:(NSString *)city stateAbbrev:(NSString *)stateAbbreviation postalCode:(NSString *)postalCode distanceFromGPSZip:(NSString *)distance country:(NSString *)country stateFullName:(NSString *)stateName {

if (self = [super init]) {

    _city = city;
    _stateAbbreviation = stateAbbreviation;
    _postalCode = postalCode;
    _distance = distance;
    _country = country;
    _stateName = stateName;
}

return self;
}

-(NSString *)description {

return [NSString stringWithFormat:@"City=%@, State=%@  Zip=%@ is %@ from the original zipCode queried", self.city, self.stateAbbreviation, self.postalCode, self.distance];
}

-(NSString *)postalCode {

return self.postalCode; // This is where I get a rather long loop...
}

@end

-(void)loadData:(NSString *)zipCode {

NSLog(@"GetSurroundingZipCodes is in loadData...");

self.zipCodes = [NSMutableArray array];

NSURL *url = nil;

NSLog(@"Get surrounding zip codes url is: %@", url);
NSString *zipCode = @"92675";
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.geonames.org/findNearbyPostalCodesJSON?postalcode=%@&country=US&radius=10&username=frequentz", zipCode]];

NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
NSError *error = nil;
NSURLResponse * response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

self.zipCodes = [NSMutableArray array];

id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

if (jsonObject != nil && error == nil) {

    NSLog(@"Successfully deserialized...");

    if ([jsonObject isKindOfClass:[NSDictionary class]]) {

        NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
        NSLog(@"Deserialized JSON Dictionary = %@", deserializedDictionary);

        for (NSString* key in deserializedDictionary) {

            id value = [deserializedDictionary objectForKey:key];

            for (NSDictionary *item in value) {

                NSString *city = [item objectForKey:@"adminName2"];
                NSString *stateAbbreviation = [item objectForKey:@"adminCode1"];
                NSString *postalCode = [item objectForKey:@"postalCode"];
                NSString *distance = [item objectForKey:@"distance"];
                NSString *country = [item objectForKey:@"country"];
                NSString *stateName = [item objectForKey:@"stateName"];

                ZipCodes *zipCode = [[ZipCodes alloc] initWithName:city stateAbbrev:stateAbbreviation postalCode:postalCode distanceFromGPSZip:distance country:country stateFullName:stateName];

                [self.zipCodes addObject:zipCode];
            }
        }
    }
}
else if (error != nil){
    NSLog(@"An error happened while deserializing the JSON data.");
}

// Assign zip codes to first five _zipCode1..5 variables
for (int i = 1; i <= 5; i++) {

    if (i == 1) {
        // If I comment out the return of zipCode in the ZipCode.m file above the loop goes away and I get a string description that looks like this: self.zipCodes: City=San Francisco, State=CA  Zip=94123 is 1.59213 from the original zipCode queried at index: 1
        NSLog(@"self.zipCodes: %@ at index: %i", self.zipCodes[i], i);  

        // Here I get the following error:  -[ZipCodes objectForKey:]: unrecognized selector sent to instance
        _zipCode1 = [[self.zipCodes objectAtIndex:i] objectForKey:@"postalCode"];

    } else if (i == 2) {
        _zipCode2 = [[self.zipCodes objectAtIndex:i] objectForKey:@"Zip"];
    } //etc...//
}
}
4

2 に答える 2

1

削除するだけです:

-(NSString *)postalCode {

return self.postalCode; // This is where I get a rather long loop...
}

取得しているループは、ゲッターが自分自身を呼び出しているためです(self.postalCodeこのメソッドを呼び出すだけです)。ゲッターを手動で定義する必要のないプロパティがあるため、自動的に合成されます。

何らかの理由でゲッターを手動で定義したい場合は、次のようにします。

-(NSString *)postalCode {

return _postalCode;
}
于 2012-10-30T23:22:15.933 に答える
0
-(NSString *)postalCode {

    return self.postalCode; // This is where I get a rather long loop...
}

あなたの問題です。self.postalCodeは同じ関数を呼び出すだけです。変数が合成されるため、実際にはゲッターは必要ありません。何かを追加するなどの理由で上書きされたゲッターが必要な場合は、次のようにすることができます。

-(NSString *)postalCode {
    NSString *postCodeOut = postalCode;
    //do something to postalCodeOut
    return postalCodeOut; 
}
于 2012-10-30T23:24:59.547 に答える