Reputation: 3711
Utilizing AFNetworking 2.5 to populate an array filled with artists pertaining to the top tracks with the last.fm API has been a challenge so far. Here is my current code for doing so.
NSURL *url = [NSURL URLWithString:URLstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dict = (NSDictionary *)responseObject;
NSArray *allValues = [dict allValues];
NSLog(@"all values contained in array: %@", allValues);
NSLog(@"all values count: %lu", allValues.count);
The count returns 1. I want to have an array populated with tracks and each indexed value corresponds to a track. Currently, only the 1st indexed value is filled within the array. Im also noticing that the JSON data being printed back is in JSON and does not appear like objects in an NSArray would appear when printed.
EDIT: here is the input
Upvotes: 2
Views: 588
Reputation: 12230
Use KVC to obtain array of tracks:
NSArray* tracks = [dict valueForKeyPath:@"toptracks.track"]
or something like that
Upvotes: 2
Reputation: 212
Judging from the input JSON that you posted the best way to achieve what you are after is to modify the following.
Change NSArray *allValues = [dict allValues];
to
NSDictionary *dict = (NSDictionary *)responseObject;
NSArray *allValues = dict[@"toptracks"][@"track"];
Upvotes: 1