生命是什么呢?生命是時時刻刻不知如何是好。
nullable 與 nonnull 的區別
__nullable
:表示對象可以是 NULL 或者 nil。
__nonnull
:表示對象不可以為空。
__null_unspecified
:表示對象未指定、不明確。
如果沒有顯式聲明,則表示 __nonnull
或者_Null_unspecified
(不明確)
在 Xcode 7 中,為了避免與第三方庫的沖突,Apple 把 __nullable
/ __nonnull
改成了 _Nullable
/_Nonnull
,也支持沒有下劃線的寫法:nullable
/ nonnull
。
使用規范:
- 對于屬性、方法返回值、方法參數的修飾,使用
nonnull
/nullable
。 - 對于 C 函數的參數、Block 的參數、Block 返回值的修飾,使用
_Nonnull
/_Nullable
,建議棄用__nonnull
/__nullable
。
為安全起見,Apple 制定的使用規則:
- 通過 typedef 定義的類型的 nullability 特性通常依賴于上下文,即使是在 Audited Regions 中,也不能假定它為
nonnull
; - 對于復雜的指針類型(如 id * )必須顯式去指定是
nonnull
還是nullable
。例如,指定一個指向nullable
對象的nonnull
指針,可以使用__nullable id * __nonnul
; - 我們經常使用的
NSError **
通常是被假定為一個指向nullable
NSError 對象的nullable
指針。
使用區別
共有三種用法:
-
nonnull
,nullable
,null_unspecified
-
_Nonnull
,_Nullable
,_Null_unspecified
, -
__nonnull
,__nullable
,__null_unspecified
。
這幾種用法在聲明時的位置區別:
- 單下劃線和雙下劃線的需要放在類型定義之后。
- 無下劃線的需要放在類型定義之前。
使用示例:
聲明屬性
@property (nonatomic, copy, nullable) NSString *name;
@property (nonatomic, copy) NSString *__nullable firstName;
@property (nonatomic, copy) NSString *_Nullable lastName;
修飾方法返回值
- (nullable NSString *)method1;
- (NSString *__nullable)method2;
- (NSString *_Nullable)method3;
修飾方法參數
- (void)methodWithString1:(nullable NSString *)aString;
- (void)methodWithString2:(NSString *__nullable)aString;
- (void)methodWithString3:(NSString *_Nullable)aString;
例外情況
??雙指針類型對象、Block 的返回值、Block 的傳入參數等,不能用 nonnull
/nullable
修飾,只能用帶下劃線的 __nonnull
/__nullable
或者 _Nonnull
/_Nullable
。
- (void)methodwithError1:(NSError * _Nullable * __null_unspecified)error;
- (void)methodwithError2:(NSError * nullable * null_unspecified)error;
Block 返回值
- (void)methodWithBlock1:(nullable void(^)(void))block;
// ?? 以上 nullable 修飾的是方法傳入參數,表示傳入的 Block 可以為空,而不是修飾 BlocK 返回值。
- (void)methodWithBlock2:(void (^ _Nullable)(void))block;
- (void)methodWithBlock3:(void (^ __nullable)(void))block;
Block 傳入參數
- (void)methodWithBlock21:(nullable id nonnull(^)(id nullable parameters))block;
// ?? 以上 nullable 修飾的是方法傳入參數,表示傳入的 BlocK 可以為空。而 __nonnull 用于修飾 Block 返回值 id 不能為空。
- (void)methodWithBlock22:(id nonnull(^ nullable)(id _Nullable parameters))block;
- (void)methodWithBlock23:(id _Nonnull (^ _Nonnull)(id _Nullable parameters))block;
宏 NS_ASSUME_NONNULL_BEGIN 與 NS_ASSUME_NONNULL_END
#import <Foundation/Foundation.h>
// 在這兩個宏之間的代碼,所有簡單指針對象都被假定為 nonnull ,因此我們只需要去指定那些 nullable 指針對象即可。
NS_ASSUME_NONNULL_BEGIN
@interface MyObject : NSObject
// 默認為 nonnull
@property (nonatomic, copy) NSString *aString;
// 方法返回值默認為 nonnull
// 方法的參數顯式聲明為 nullabel,表示入參可以為空
- (id)methodWithString:(nullable NSString *)string;
@end
NS_ASSUME_NONNULL_END