Skip to content

Instantly share code, notes, and snippets.

@HeidiHansen
Created September 12, 2014 20:06
Show Gist options
  • Save HeidiHansen/dc55d705cb2d95f24713 to your computer and use it in GitHub Desktop.
Save HeidiHansen/dc55d705cb2d95f24713 to your computer and use it in GitHub Desktop.
Extracting titles from an array of book dictionaries
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *arrayOfBooks = [[NSMutableArray alloc]init];
[MPSAPI getBookLibrary:^(NSArray *books) {
for (NSMutableDictionary *book in books) {
NSString *bookTitle = book[@"title"];
[arrayOfBooks addObject:bookTitle];
}
}];
}
//associated API request:
+ (void)getBookLibrary:(void (^)(NSArray *))completion {
NSOperationQueue *backgroundqueue =[NSOperationQueue new];
NSString *getURL = [NSString stringWithFormat:@"%@", kMAIN_API_URL];
NSLog(@"%@", getURL);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:getURL
parameters:nil
success:^(AFHTTPRequestOperation *task, id responseObject) {
NSLog(@"%@", responseObject);
[backgroundqueue addOperationWithBlock:^{
completion(responseObject);
}];
}failure:^(AFHTTPRequestOperation *task, NSError *error) {
NSLog(@"Failed: %@", error.localizedDescription);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment