Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to get zipcode from local populated class into variable but getting a nasty loop
    primarykey
    data
    text
    <p>I have the following class called ZipCode and a loadData method that successfully gets data from a third party service and populates my local instantiated ZipCode class. The problem I am having is getting the populated values in this class into 5 local variables I can then pass on to my stored procedure which pulls back from the database the stores surrounding the entered zip code. </p> <p>ZipCode.h</p> <pre><code>@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 </code></pre> <p>ZipCode.m</p> <pre><code>@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 </code></pre> <p>-(void) loadData:(NSString *)zipCode {</p> <pre><code>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=%@&amp;country=US&amp;radius=10&amp;username=frequentz", zipCode]]; NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url]; NSError *error = nil; NSURLResponse * response = nil; NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&amp;response error:&amp;error]; self.zipCodes = [NSMutableArray array]; id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&amp;error]; if (jsonObject != nil &amp;&amp; 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 &lt;= 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...// } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload