BookBuilder.m 1.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
//
//  BookBuilder.m
//  WAMI
//
//  Created by PLUC SeniorDesign on 3/31/15.
//  Copyright (c) 2015 BucknellECE_SeniorDesignTeam1. All rights reserved.
//

#import "BookBuilder.h"
#import "Book.h"

@implementation BookBuilder

+ (NSArray *)booksFromJSON:(NSData *)objectNotation error:(NSError **)error
{
    NSError *localError = nil;
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];
    
    if (localError != nil) {
        *error = localError;
        return nil;
    }
    
    NSMutableArray *books = [[NSMutableArray alloc] init];
    
    NSArray *results = [parsedObject valueForKey:@"books"];
    
    for (NSDictionary *bookDic in results) {
        Book *book = [[Book alloc] init];
        
        for (NSString *key in bookDic) {
            if ([book respondsToSelector:NSSelectorFromString(key)]) {
                [book setValue:[bookDic valueForKey:key] forKey:key];
            }
        }
        
        [books addObject:book];
    }
    
    return books;
}

@end