-
Notifications
You must be signed in to change notification settings - Fork 2
/
AnswerBuilder.m
55 lines (48 loc) · 1.99 KB
/
AnswerBuilder.m
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
47
48
49
50
51
52
53
54
55
//
// AnswerBuilder.m
// BrowseOverflow
//
// Created by Graham Lee on 25/04/2011.
// Copyright 2011 Fuzzy Aliens Ltd. All rights reserved.
//
#import "AnswerBuilder.h"
#import "Answer.h"
#import "Question.h"
#import "UserBuilder.h"
@implementation AnswerBuilder
- (BOOL)addAnswersToQuestion: (Question *)question fromJSON: (NSString *)objectNotation error: (NSError **)error {
NSParameterAssert(objectNotation != nil);
NSParameterAssert(question != nil);
NSData *unicodeNotation = [objectNotation dataUsingEncoding: NSUTF8StringEncoding];
NSError *localError = nil;
NSDictionary *answerData = [NSJSONSerialization JSONObjectWithData: unicodeNotation options: 0 error: &localError];
if (answerData == nil) {
if (error) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity: 1];
if (localError != nil) {
[userInfo setObject: localError forKey: NSUnderlyingErrorKey];
}
*error = [NSError errorWithDomain: AnswerBuilderErrorDomain code: AnswerBuilderErrorInvalidJSONError userInfo: userInfo];
}
return NO;
}
NSArray *answers = [answerData objectForKey: @"answers"];
if (answers == nil) {
if (error) {
*error = [NSError errorWithDomain: AnswerBuilderErrorDomain code:AnswerBuilderErrorMissingDataError userInfo: nil];
}
return NO;
}
for (NSDictionary *answerData in answers) {
Answer *thisAnswer = [[Answer alloc] init];
thisAnswer.text = [answerData objectForKey: @"body"];
thisAnswer.accepted = [[answerData objectForKey: @"accepted"] boolValue];
thisAnswer.score = [[answerData objectForKey: @"score"] integerValue];
NSDictionary *ownerData = [answerData objectForKey: @"owner"];
thisAnswer.person = [UserBuilder personFromDictionary: ownerData];
[question addAnswer: thisAnswer];
}
return YES;
}
@end
NSString *AnswerBuilderErrorDomain = @"AnswerBuilderErrorDomain";