如何根据NSDictionary参数创建单个变量?

时间:2015-04-14 14:38:36

标签: objective-c github-mantle

来自服务器的数据

{
    "type": "reward",
    "stamp_pos": "1",
    "reward_id": "350",
    "reward_title": "dfddffd",
    "reward_cost": "1",
    "reward_cost_text": "Stamp Reward",
    "reward_tnc": "12pm between Monday - Thursday.",
    "member_reward_tracker_id": "1180",
    "redeem_now": "1",
    "redeemed": "0",
    "active": "1",
    "redeemed_date": "0000-00-00 00:00:00",
    "reward_featured_image": "rs/img/someimage"
},

我想做的是

我的Model.h

typedef NS_ENUM(NSInteger, CardType) {
    CardTypeStampAvailable = 1,
    CardTypeStampRedeemed,
    CardTypeRewardAvailable,
    CardTypeRewardRedeemNow,
    CardTypeRewardRedeemed,
};
@interface ModelCardReward : MTLModel <MTLJSONSerializing>

@property(nonatomic) CardType myCardType;
@property(nonatomic, strong) NSString *rewardTitle
@property(nonatomic, strong) NSString *rewardCostText;
@end

我的Model.m

@implementation ModelCard

+ (NSDictionary *)JSONKeyPathsByPropertyKey {

    return @{
         @"rewardTitle" : @"reward_title",
         @"rewardCostText" : @"reward_cost",
         };
}

我希望变量myCardType具有NS_ENUM值 基于字典参数。

那是

if ([json[@"type"] isEqualToString:reward] && [json[@"redeem_now"] isEqualToString:@"1"]) {

    myCardType = CardTypeRewardAvailable;
}

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

您可以使用自定义NSValueTransformer Mantle 2 中实现此目的,并将您的属性映射到多个JSON字段。下面的例子应该很清楚。

ModelCardReward.h

typedef NS_ENUM(NSInteger, CardType) {
    CardTypeStampAvailable = 1,
    CardTypeStampRedeemed,
    CardTypeRewardAvailable,
    CardTypeRewardRedeemNow,
    CardTypeRewardRedeemed,
};

@interface ModelCardReward : MTLModel <MTLJSONSerializing>
@property(nonatomic) CardType myCardType;
@property(nonatomic, strong) NSString *rewardTitle;
@property(nonatomic, strong) NSString *rewardCostText;
@end

ModelCardReward.m

#import "ModelCardReward.h"

@implementation ModelCardReward

+ (NSDictionary *)JSONKeyPathsByPropertyKey {

    return @{
        @"myCardType" : @[ @"type", @"redeem_now" ],
        @"rewardTitle" : @"reward_title",
        @"rewardCostText" : @"reward_cost",
    };
}

+ (NSValueTransformer *)myCardTypeJSONTransformer {
    return [MTLValueTransformer transformerUsingForwardBlock:^NSNumber *(NSDictionary *value, BOOL *success, NSError *__autoreleasing *error) {

        if ([value[@"type"] isEqualToString:@"reward"] && [value[@"redeem_now"] isEqualToString:@"1"]) {
            return @(CardTypeRewardAvailable);
        }

        //logic for other enum values

        //return some default value
        return @(CardTypeRewardRedeemed);

    }];
}

@end

致电代码:

NSDictionary *dict = @{
    @"type": @"reward",
    @"stamp_pos": @"1",
    @"reward_id": @"350",
    @"reward_title": @"dfddffd",
    @"reward_cost": @"1",
    @"reward_cost_text": @"Stamp Reward",
    @"reward_tnc": @"12pm between Monday - Thursday.",
    @"member_reward_tracker_id": @"1180",
    @"redeem_now": @"1",
    @"redeemed": @"0",
    @"active": @"1",
    @"redeemed_date": @"0000-00-00 00:00:00",
    @"reward_featured_image": @"rs/img/someimage"
};

NSError *error = nil;
ModelCardReward *model = [MTLJSONAdapter modelOfClass:ModelCardReward.class fromJSONDictionary:dict error:&error];
if (error) {
    NSLog(@"Error creating model from JSON is %@", error);
} else {
    NSLog(@"Model is %@", model);
}