JSONModel是一個解析伺服器返回的Json資料的庫。
通常伺服器傳回的json資料要通過寫一個資料轉換模組將NSDictionary轉換為Model,將NSString資料轉換為Model中property的資料類型。
這樣伺服器如果要做修改,可能需要改兩三個檔案。
JSONModel的出現就是為了將這種解析工作在設計層面完成。
使用方法:參考串連
對其源碼的核心部分JSONModel.m做了源碼閱讀,筆記如下:
在
函數中完成所有解析工作:如果有任何失誤或者錯誤直接返回nil。
-()initWithDictionary:(NSDictionary*)dict error:(NSError** (! (err) *err = (![dict isKindOfClass:[NSDictionary (err) *err = self = (! (err) *err = NSArray* incomingKeysArray =* requiredProperties =* incomingKeys = JSONKeyMapper* keyMapper = NSMutableSet* transformedIncomingKeys =* transformedName = (NSString* requiredPropertyName transformedName = incomingKeys = (! JMLog( (err) *err = incomingKeys== (JSONModelClassProperty* property NSString* jsonKeyPath = (keyMapper) jsonKeyPath = jsonValue = (jsonValue==nil && property.isOptional== Class jsonValueClass = [jsonValue = (Class allowedType = (isValueOfAllowedType== JMLog( (err) *err = (property.type == nil && property.structName== ([[property.type ] isSubclassOfClass:[JSONModel NSError* initError = value = [[property.type alloc] initWithDictionary: jsonValue error:& (! (initError && err) *err = jsonValue = (! (err) *err = (property.isStandardJSONType && = (![jsonValue isKindOfClass:property.type] && !|| Class sourceClass = [JSONValueTransformer classByResolvingClusterClasses:[jsonValue NSString* selectorName = [NSString stringWithFormat:? property.structName : property.type), sourceClass]; SEL selector = clang diagnostic push clang diagnostic ignored "-Warc-performSelector-leaks" jsonValue = clang diagnostic pop [NSException exceptionWithName:, property.type, [self NSError* validationError == [self validate:& (doesModelDataValidate == (err) *err =