iOS9新特性之常见关键字

iOS9新出现的关键字:nullable,nonnull,null_resettable,_Null_unspecified等使用来修饰属性,或者方法的参数,返回值。

它们出现的好处是:1、迎合swift的特性;2、更加规范开发人员,同时减少程序员之间的沟通成本。

下面具体介绍一下新出现的这些关键字。


nullable

作用:表示可以为空

特点:只能修饰对象,不能修饰基本数据类型

书写规范:

1、修饰成员变量

 // 方式一:
 @property (nonatomic, strong, nullable) NSString *name;
 // 方式二:
 @property (nonatomic, strong) NSString *_Nullable name;
 // 方式三:
 @property (nonatomic, strong) NSString *__nullable name;

2、修饰方法的返回值或参数

- (nullable NSString *)test:(nullable NSString *)str;
- (NSString * _Nullable)test1:(NSString * _Nullable)str;


nonnull

作用:标识非空

特点:只能修饰对象,不能修饰基本数据类型

书写规范:

1、修饰成员变量

// 方式一
@property (nonatomic, strong, nonnull) NSString *icon;
 // 方式二
 @property (nonatomic, strong) NSString * _Nonnull icon;
 // 方式三
 @property (nonatomic, strong) NSString * __nonnull icon;
2、修饰方法的返回值或参数

- (nonnull NSString *)test:(nonnull NSString *)str;
- (NSString * _Nonnull)test1:(NSString * _Nonnull)str;



_Null_unspecified

作用:不确定是否为空

书写规范:

    // 方式一
    @property (nonatomic, strong) NSString *_Null_unspecified name;
    // 方式二
    @property (nonatomic, strong) NSString *__null_unspecified name;
 


null_resettable

作用:处理set方法传递空值得情况,使用了这个属性修饰成员变量,get方法不能返回空,setter方法可以为空

特点:使用null_resettable修饰成员变量,必须重写get方法或者set方法,处理传递值为空的情况

书写规范:

@property (nonatomic, strong, null_resettable) NSString *name;




你可能感兴趣的:(iOS编程)