1

我有以下 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 字符串时

测试模型.h

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

测试模型.m

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

我想如何将西装从字符串转换为具有多个 url 的 NSArray,所以我做了以下操作:

测试模型.h

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

测试模型.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

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

4

1 回答 1

0

应该调用返回NSValueTransformerfor的方法,而不是。suitssuitsJSONTransformersuitsJSONTransform

方法名称的格式是<propertyName>JSONTransformer.

于 2015-11-12T10:46:44.730 回答