iOS9 新特性 常见关键字

来源:互联网 发布:淘宝达人自我简述 编辑:程序博客网 时间:2024/04/29 15:34

nullable,

nonnull,

null_resettable,

_Null_unspecified等使用来修饰属性,或者方法的参数,返回值。

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


nullable

作用:表示可以为空

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


1、修饰成员变量


@property (nonatomicstrong, nullable) NSString *name;

@property (nonatomicstrongNSString *_Nullable name; 

@property (nonatomicstrongNSString *__nullable name;


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


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

nonnull

作用:标识非空

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


1、修饰成员变量

[objc] view plain copy
  1. // 方式一  
  2. @property (nonatomicstrong, nonnull) NSString *icon;  
  3.  // 方式二  
  4.  @property (nonatomicstrongNSString * _Nonnull icon;  
  5.  // 方式三  
  6.  @property (nonatomicstrongNSString * __nonnull icon;</span>  
2、修饰方法的返回值或参数

[objc] view plain copy
  1. - (nonnull NSString *)test:(nonnull NSString *)str;  
  2. - (NSString * _Nonnull)test1:(NSString * _Nonnull)str;</span>  



_Null_unspecified

作用:不确定是否为空

书写规范:

[objc] view plain copy
  1.   // 方式一  
  2.     @property (nonatomicstrongNSString *_Null_unspecified name;  
  3.     // 方式二  
  4.     @property (nonatomicstrongNSString *__null_unspecified name;  
  5.  


null_resettable

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

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

书写规范:

[objc] view plain copy
  1. @property (nonatomicstrong, null_resettable) NSString *name;