I'm new to Objective-C, just wondering how to use NSArray
object outside from JSON.
For example:
NSURL *url = [NSURL URLWithString:@"http://acumen-locdef.elasticbeanstalk.com/service/countries"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSMutableArray *myFinalListArray = [[NSMutableArray alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError)
{
if (data.length > 0 && connectionError == nil)
{
NSMutableArray *greeting = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
for (NSDictionary *countryList in greeting) {
[myFinalListArray addObject:countryList[@"name"]];
}
}
NSLog(@"%@",myFinalListArray); //(This one showing all results..)
}];
NSLog(@"%@",myFinalListArray); //(This one giving empty result)
I have defined myFinalListArray
and added objects in for loop.
If you use NSLog
inside the loop or outside the loop it will show you results. But if I use this after }];
(after the code is ending.),
it's giving me empty array.