I am very new with ios dev and I have been struggling with almost everything so far. It’s really very frustrating really.
I have put the comment /code crashes here/ to indicate where my app flow stops. I am requesting the json data with
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.2.4/iRestaurant/users"]];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
The implementation for fetchedData
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
//NSLog(@"%@", json);
NSArray* latestLoans = [json objectForKey:@"Users"]; //2
NSEnumerator* enumerator = [latestLoans objectEnumerator];
id element;
while(element = [enumerator nextObject]) {
Result *fetchedResults = [Result init];
/*crashes here*/
fetchedResults.name = [[element objectForKey:@"User"] objectForKey:@"name"];
fetchedResults.email = [[element objectForKey:@"User"] objectForKey:@"name"];
[results addObject:fetchedResults];objectForKey:@"name"]);
}
}
Result is a custom object and is a subclass of NSObject. I have imported Result.h. I have also declared NSMutableArray *results in my current file’s declaration file. I am pushing the objects into results. The header file for Result looks like
@interface Result : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, assign) int ID;
@end
I will be implementing cellForRowAtIndexPath and pushing the contents of results into the reusable cell.
My JSON response is
{"Users":
[{"User":
{"id":"1",
"name":"Kishor kundan",
"email":"kis@kun.ca",
"password":"asdfasdf",
"fb_id":"1234444"
}
},
{"User":
{"id":"2",
"name":"adsfasdf",
"email":"asdfasdf@asdf.asdf",
"password":"asdfasdf",
"fb_id":"123123"
}
}]
}
The code was crashing due to object not found. My code was referencing a location that was not there. I took @btype’s suggestion from my other question