How to make primitive type properties Optional?

我与影子孤独终老i 提交于 2019-11-29 07:27:52

问题


I want to make some primitive properties option in my JSONModel classes. Please see the code below.

#import "JSONModel.h"

@protocol GreenModel <NSObject>
@end

@interface MyModel : JSONModel

@property (nonatomic, assign) NSInteger<Optional> objId;
@property (nonatomic, strong) NSString *name;
@end

Can anybody suggest a way to achieve this?


回答1:


You can do this by using propertyIsOptional:. Just return YES for the names of the properties you want to make Optional.

https://github.com/icanzilb/JSONModel#make-all-model-properties-optional-avoid-if-possible

+(BOOL)propertyIsOptional:(NSString*)propertyName
{
  if ([propertyName isEqualToString: @"objId"]) return YES;
  return NO;
}



回答2:


For swift

Please use following code in the sub class of your JSON model. If you want to give all the properties as optional then the code will looks like this:

override class func propertyIsOptional(propertyName: String!) -> Bool    {
   return true
}    

If you want a specific property the code will looks like this:

override class func propertyIsOptional(propertyName: String!) -> Bool     {
if propertyName == "your_property_name"
{
    return true
}
    return false
}


来源:https://stackoverflow.com/questions/21778271/how-to-make-primitive-type-properties-optional

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!