1

サーバーからのデータ

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

私がやろうとしていることは

私のモデル.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

私のモデル.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;
}

どんな助けでも素晴らしいでしょう。

4

1 に答える 1

0

これは、 Mantle 2でカスタムNSValueTransformerを使用し、プロパティを複数の 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);
}
于 2015-04-14T16:20:20.793 に答える