1

I have the following json string:

{"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

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

How ever I would like to convert suits from string to a NSArray with multiple urls so I did following:

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

But it's not working. The result is nil. When I override the wrong function?

user2507194
  • 175
  • 4
  • 13

1 Answers1

0

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

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

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132