ios 關(guān)鍵字 每天學(xué)一點(diǎn)

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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容