如何在Mantle中声明可选属性?

时间:2017-04-02 10:52:28

标签: ios objective-c jsonserializer github-mantle

具体情况详情:

在我目前的照片应用中,有这样的情况:首先允许用户体验而无需登录, 但JSON中没有一些字段(例如:投票,购买等)从后端回复。如果用户已登录, 该字段以JSON格式添加。

我将展示我的工具详情如下:

PhotoListResponseModel(直接继承MTLModel)

@interface PhotoListResponseModel : MTLModel <MTLJSONSerializing>
...
@property (nonatomic, copy, readonly) NSNumber *foo;
@property (nonatomic, copy, readonly) NSArray<PhotoModel *> *subPhotos;

@end

@interface PhotoModel : MTLModel <MTLJSONSerializing>

@property (nonatomic, copy, readonly) NSNumber *photoID;
@property (nonatomic, copy, readonly) NSURL *imageUrl;
...
@end


@implementation PhotoListResponseModel
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
   return @{@"foo": @"foo"
            @"subPhotos": @"photos"
            };
}

+ (NSValueTransformer *)subPhotosJSONTransformer {
    return [MTLJSONAdapter arrayTransformerWithModelClass:PhotoModel.class];
}

@end

@implementation PhotoModel
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"photoID": @"id",
             @"userID": @"user_id",
             };
}

@end

相应地, JSON示例如下:

noAuthPhoto.json

{
        "foo": 1,
        "photos": [
                   {
                   "id": 204748881,
                   "image_url": "https://foo1/bar1",
                   },
                   {
                   "id": 204996257,
                   "image_url": "https://foo2/bar2"
                   }
                   ],
                   …
                   …
    }

AuthedPhoto.json

{
        "foo": 1,
        "photos": [
                   {
                   "id": 204748881,
                   "image_url": "https://foo1/bar2”,
                   "voted": false,
                   "purchased": false
                   },
                   {
                   "id": 204996257,
                   "image_url": "https://foo2/bar2”,
                   "voted": false,
                   "purchased": false
                   }
                 ],
                 ...
                 ...
    }

那么,如何让新字段与我现有的代码兼容

可选属性? (我不知道该怎么做。)

或者从PhotoListResponseModel&amp;添加继承的子类。 PhotoModel?

或者任何好主意:))

更新:我注意到this information,但仍然不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

可选属性仅在Swift中可用,但Obj-C的类似想法 - 可能具有nil值的对象 - 应该可以正常工作。您可以将以下NSNumber属性添加到PhotoModel

@property (nonatomic, strong) NSNumber *voted;
@property (nonatomic, strong) NSNumber *purchased;

PhotoModel对象创建过程中,从解析后的JSON中分配BOOL值,如下所示:

photoModelObject.voted = [NSNumber numberWithBool:[parsedJson valueForKey:@"voted"]];
photoModelObject.purchased = [NSNumber numberWithBool:[parsedJson valueForKey:@"purchased"]];

接下来,当您引用照片对象时,只需检查这些属性是否具有nil值。如果是 - 您的用户未登录,您也不需要显示它们。否则,从它们中提取BOOL值,如下所示:

if (photoModelObject.voted && photoMobelObject.purchased) {
    BOOL isVoted = [photoModelObject.voted boolValue];
    BOOL isPurchased = [photoModelObject.purchased boolValue];

    // Use booleans to present info for registered users
} else {
    // There is no info about votes and purchasing provided for the current user
}