SectionBuilder.m 1.15 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 44 45 46
//
//  SectionBuilder.m
//  WAMI
//
//  Created by PLUC SeniorDesign on 4/4/15.
//  Copyright (c) 2015 BucknellECE_SeniorDesignTeam1. All rights reserved.
//

#import "SectionBuilder.h"
#import "Section.h"

@implementation SectionBuilder

+ (NSArray *)sectionsFromJSON:(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 *sections = [[NSMutableArray alloc] init];
    
    NSArray *results = [parsedObject valueForKey:@"options"];
    
    NSUInteger count = 0;
    
    for (NSDictionary *sectionDic in results) {
        Section *section = [[Section alloc] init];
        
        for (NSString *key in sectionDic) {
            if ([section respondsToSelector:NSSelectorFromString(key)]) {
                [section setValue:[sectionDic valueForKey:key] forKey:key];
            }
        }
        section.uid = count;
        count++;
        [sections addObject:section];
    }
    
    return sections;
}

@end