13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

306CHAPTER 9: Writing an ApplicationListing 9-38. Guaranteeably JSON-Encoding an NSError Object+ (NSError *) errorWithJSONDictionaryRepresentation: (NSDictionary *) dictionary{NSMutableDictionary * userInfo = [dictionary[APErrorDictionaryUserInfoKey] mutableCopy];if ( userInfo != nil ){NSArray * keys = [[userInfo allKeys] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock: ^BOOL(id evaluatedObject, NSDictionary *bindings) {return ( [evaluatedObject hasPrefix: APErrorEncodedKeyPrefix] );}]];// declared __strong so we can modify key in ARC codefor ( __strong NSString * key in keys ){NSString * encoded = userInfo[key];[userInfo removeObjectForKey: key];}}// get the original key...key = [key substringFromIndex: [APErrorEncodedKeyPrefix length]];// ... and the original valueuserInfo[key] = Base64Decode(encoded);}return ( [NSError errorWithDomain: dictionary[APErrorDictionaryDomainKey]code: [dictionary[APErrorDictionaryCodeKey] integerValue]userInfo: userInfo] );- (NSDictionary *) jsonDictionaryRepresentation{NSMutableDictionary * dict = [NSMutableDictionary new];dict[APErrorDictionaryCodeKey] = @([self code]);dict[APErrorDictionaryDomainKey] = [self domain];if ( [self userInfo] != nil ){dict[APErrorDictionaryUserInfoKey] = [self userInfo];}if ( [NSJSONSerialization isValidJSONObject: dict] == NO )dict[APErrorDictionaryUserInfoKey] = EncodedDictionary([self userInfo]);}return ( [dict copy] );Encoding CommandsThe commands sent across the network correspond directly to the four methods of theAPAddressBook protocol: fetch all users; fetch addresses; fetch e-mails; and fetch phonenumbers. These methods all return arrays, which can be easily encoded into JSON data.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!