iOS9新特性 關(guān)鍵字
隨著iOS的發(fā)展,蘋果也開始為了程序猿一看別人代碼就懂怎么賦值 也開始規(guī)范寫法了
下面先介紹下關(guān)鍵字
nonull nullable null_resettable null_unspecified
這些關(guān)鍵字:修飾屬性,方法的參數(shù),方法反回值.
**注意:只能用于聲明對象,不能聲明基本數(shù)據(jù)類型int double,因為只有對象才能為nil.
nonull 與 nullable
nonnull:表示屬性不能為空,non:非,null:空,那就是非空的意思
方式一:
@property (nonatomic, strong, nonnull) NSString *name;
方式二:
@property (nonatomic, strong) NSString * _Nonnull name;
方式三:
@property (nonatomic, strong) NSString * __nonnull name;
###nullable:可以為nil
方式一:
@property (nonatomic, strong, nullable) NSString *name;
方式二:
@property (nonatomic, strong) NSString * _Nullable name;
方式三:
@property (nonatomic, strong) NSString * __nullable name;
在NS_ASSUME_NONNULL_BEGIN與NS_ASSUME_NONNULL_END之間所有的對象屬性,方法參數(shù),方法返回值,默認(rèn)都是nonnul。
NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong) NSString *name;
NS_ASSUME_NONNULL_END
@property (nonatomic, strong, null_resettable) NSString *name;
### null_resettable:可以重新設(shè)置空,set方法可以為空,get不能為空。
注意:用null_resettable屬性,必須重寫set,或者get方法,處理傳值為nil的情況,可以模仿控制器view的get方法,當(dāng)view為nil,就自己創(chuàng)建一個.
_Null_unspecified:不確定是否為空.
@property (nonatomic, strong) NSString * _Null_unspecified name;