简体   繁体   English

如何使用Mantle自定义NSdictionary?

[英]How can I customize the NSdictionary using Mantle?

I have the following json string: 我有以下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"}

So when use mantle to parse the json string with following file 因此,当使用披风解析以下文件的json字符串时

testModel.h testModel.h

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

testModel.m testModel.m

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

How ever I would like to convert suits from string to a NSArray with multiple urls so I did following: 我如何将西装从字符串转换为具有多个URL的NSArray,所以我做了以下工作:

testModel.h testModel.h

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

testModel.m 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

But it's not working. 但这不起作用。 The result is nil. 结果为零。 When I override the wrong function? 什么时候我覆盖了错误的功能?

The method returning the NSValueTransformer for suits should be called suitsJSONTransformer , not suitsJSONTransform . 返回NSValueTransformer作为suits的方法应称为suitsJSONTransformer ,而不是suitsJSONTransform

The format for the method name is <propertyName>JSONTransformer . 方法名称的格式为<propertyName>JSONTransformer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM