如何使用Mantle自定义NSdictionary?

时间:2015-11-09 16:42:47

标签: objective-c json nsdictionary github-mantle

我有以下json字符串:

{"suit_id": 2427;
"suits": "http://img.prettyyes.com/1137-4930-1446175512.jpeg;http://img.prettyyes.com/1137-7665-1446175512.jpeg;http://img.prettyyes.com/1137-4783-1446175512.jpeg"}

所以当使用mantle用以下文件解析json字符串时

testModel.h

@interface testModel : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSString *suit_id;
@property (nonatomic, strong) NSString *suits;
@end

testModel.m

@implementation testModel
+ (NSDictionary *) JSONKeyPathsByPropertyKey {
    return @{
            "suit_id":   "suit_id",
            "suits":     "suits"
            };
}
@end

我多么希望将套装从字符串转换为带有多个网址的NSArray,所以我做了以下操作:

testModel.h

@interface testModel : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSString *suit_id;
@property (nonatomic, strong) NSArray *suits;
@end

testModel.m

@implementation testModel
+ (NSDictionary *) JSONKeyPathsByPropertyKey {
    return @{
            "suit_id":   "suit_id",
            "suits":     "suits"
            };
}

+ (NSValueTransformer *) suitsJSONTransform {
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSString *str, BOOL *success, NSError **error){
        return [[str componentsSeparatedByString:@";"] mutableCopy];
    }];
}
@end

但它不起作用。结果是零。当我覆盖错误的功能?

1 个答案:

答案 0 :(得分:0)

The method returning the NSValueTransformer for suits should be called suitsJSONTransformer, not suitsJSONTransform.

The format for the method name is <propertyName>JSONTransformer.