iOS 新特性關鍵字

來源:互聯網
上載者:User

標籤:

1.用來修飾屬性,或者方法的參數,方法的傳回值

/**

 nullable:表示可以傳空

*/

//@property (nonatomic, strong, nullable) NSString *name;

//@property (nonatomic, strong) NSString * __nullable name;

//@property (nonatomic, strong) NSString * _Nullable name;

 

/**

 nonnull: non:非  null : 空

*/

 

//@property (nonatomic, strong, nonnull) NSString *icon;

//@property (nonatomic, strong) NSString * __nonnull icon;

//@property (nonatomic, strong) NSString * _Nonnull icon;

 

//方法中書寫規範

/**

 

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

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

*/

 

/**

//在 NS_ASSUME_NONNULL_BEGIN 和 NS_ASSUME_NONNULL_END 之間定義的所有屬性和方法預設都是 nonnull

NS_ASSUME_NONNULL_BEGIN

 

@property (nonatomic) NSString *name;//這樣預設表示非空

 

NS_ASSUME_NONNULL_END

 

*/

 

/**

 *  null_resettable :get方法傳回值不可為空 set方法可以為空白

 *  如果使用了這種方法必須重寫 set 方法或者重寫 get 方法處理傳遞值為空白的情況

 */

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

 

/**

 *  null_unspecified: 不確定為空白

 */

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

 

好處:

1.迎合 swift

2.提高我們開發人員開發規範,減少程式按之間交流

 

//注意 iOS9新出關鍵字 nonnull, nullable 只能修飾對象,不能修飾基礎資料型別 (Elementary Data Type)

2.iOS9 泛型

  

    /**

     泛型:限制類型

     泛型使用情境

     1.在集合(數組, 字典, NSSet)中使用比較常見

     2.當聲明一個類,類裡面的某些屬性的類型不確定,這時候我們才使用泛型

     

     

     泛型書寫規範

     @property (nonatomic, strong) NSMutableArray<NSString *> *data;

     在類型的後面定義泛型 NSMutableArray<NSString *> *data;

 

     泛型修飾:只能修飾方法的調用

     泛型好處:

     1.提高開發規範,減少程式之間交流

     2.通過集合取出來對象,直接當做泛型對象使用,可以使用點文法

     */

  1. __kindof

 /**

     * __kindof:表示當前類或者它子類

     * __kindof書寫格式

     * 放在類型前面修飾這個類型

     +(__kindof person *)person;

     __kindof : 在調用的時候很清楚的知道返回的類

     */

    

    /**

     * id 壞處: 1. 不能再編譯的時候檢查真是類型

     *          2.傳回值,沒有提示

       instancetype: 會自動識別當前對象的類,但是傳回值還是沒有提示

     */

 

iOS 新特性關鍵字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.