我有一个名为ZipCode的类和一个loadData方法,它成功地从第三方服务获取数据并填充我本地实例化的ZipCode类。我遇到的问题是将此类中的填充值转换为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...//
}
}
答案 0 :(得分:1)
只需删除:
-(NSString *)postalCode {
return self.postalCode; // This is where I get a rather long loop...
}
你得到的循环是因为你的getter正在调用自己(self.postalCode
只是调用这个方法)。由于你有一个属性,你不需要手动定义一个getter,它会自动为你合成。
如果出于任何原因想要手动定义getter,请执行以下操作:
-(NSString *)postalCode {
return _postalCode;
}
答案 1 :(得分:0)
-(NSString *)postalCode {
return self.postalCode; // This is where I get a rather long loop...
}
是你的问题。 self.postalCode只调用相同的函数。在任何情况下你都不需要getter,因为变量是合成的。如果你必须有一个覆盖的getter来说,添加一些额外的处理或其他东西,你可以只做以下。
-(NSString *)postalCode {
NSString *postCodeOut = postalCode;
//do something to postalCodeOut
return postalCodeOut;
}