[英]How to declare optional property in Mantle?
Specific situation detail: 具体情况详情:
In my current photo app, There are such situation : allow user experience first without log in, but no have some field(eg:voted,purchased ,etc) in the JSON responded from backend. 在我目前的照片应用程序中,存在这样的情况:首先允许用户体验而不登录,但是在后端响应的JSON中没有一些字段(例如:投票,购买等)。 If user logged in, the field is added in JSON . 如果用户已登录,则该字段将以JSON格式添加。
I'll show off my implement details as following: 我将展示我的工具细节如下:
PhotoListResponseModel (directly inherit MTLModel) 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
Correspondly, the JSON example as following: 相应地, JSON示例如下:
noAuthPhoto.json noAuthPhoto.json
{
"foo": 1,
"photos": [
{
"id": 204748881,
"image_url": "https://foo1/bar1",
},
{
"id": 204996257,
"image_url": "https://foo2/bar2"
}
],
…
…
}
AuthedPhoto.json 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
}
],
...
...
}
So, how let new field can compatible my already existing code ? 那么, 新字段如何兼容我现有的代码呢?
Optional Property? 可选物业? (I not know how to do this.) (我不知道该怎么做。)
Or add a subclass of inherit from PhotoListResponseModel & PhotoModel? 或者从PhotoListResponseModel和PhotoModel添加继承的子类?
Or any good idea : ) 或者任何好主意:)
Update: I noticed this information ,but still don't know how to do it. 更新:我注意到了这些信息 ,但仍然不知道该怎么做。
Optional properties are only available in Swift, but similar idea for the Obj-C — objects with possible nil
value — should work well. 可选属性仅在Swift中可用,但Obj-C的类似想法 - 具有可能的nil
值的对象 - 应该可以正常工作。 You can add the following NSNumber
properties to your PhotoModel
: 您可以将以下NSNumber
属性添加到您的PhotoModel
:
@property (nonatomic, strong) NSNumber *voted;
@property (nonatomic, strong) NSNumber *purchased;
And assign your BOOL values from parsed JSON during PhotoModel
object creation like this: 并在PhotoModel
对象创建期间从解析的JSON中分配您的BOOL值,如下所示:
photoModelObject.voted = [NSNumber numberWithBool:[parsedJson valueForKey:@"voted"]];
photoModelObject.purchased = [NSNumber numberWithBool:[parsedJson valueForKey:@"purchased"]];
Next, when you reference your photo object, just check if these properties have nil
value. 接下来,当您引用照片对象时,只需检查这些属性是否nil
值。 If they are — your user is not logged in and you don't need to show them. 如果是 - 您的用户未登录,您不需要显示它们。 Otherwise, extract BOOL value from them like this: 否则,从它们中提取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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.