如何使基本类型属性为可选? [英] How to make primitive type properties Optional?

查看:102
本文介绍了如何使基本类型属性为可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JSONModel类中添加一些原始属性选项.请参见下面的代码.

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?

推荐答案

您可以使用propertyIsOptional:来做到这一点.只需对要设置为可选的属性名称返回YES.

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;
}

这篇关于如何使基本类型属性为可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆