我有以下名为 ZipCode 的类和一个 loadData 方法,该方法成功地从第三方服务获取数据并填充我的本地实例化 ZipCode 类。我遇到的问题是将此类中的填充值放入 5 个局部变量中,然后我可以将其传递给我的存储过程,该存储过程从数据库中拉回输入的邮政编码周围的商店。
邮编.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
邮编.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...//
}
}