生命是什么呢?生命是時(shí)時(shí)刻刻不知如何是好辐马。
nullable 與 nonnull 的區(qū)別
__nullable
:表示對(duì)象可以是 NULL 或者 nil令宿。
__nonnull
:表示對(duì)象不可以為空悲酷。
__null_unspecified
:表示對(duì)象未指定岩臣、不明確烈评。
如果沒(méi)有顯式聲明,則表示 __nonnull
或者_Null_unspecified
(不明確)
在 Xcode 7 中乱凿,為了避免與第三方庫(kù)的沖突顽素,Apple 把 __nullable
/ __nonnull
改成了 _Nullable
/_Nonnull
咽弦,也支持沒(méi)有下劃線的寫(xiě)法:nullable
/ nonnull
徒蟆。
使用規(guī)范:
- 對(duì)于屬性、方法返回值型型、方法參數(shù)的修飾段审,使用
nonnull
/nullable
。 - 對(duì)于 C 函數(shù)的參數(shù)闹蒜、Block 的參數(shù)寺枉、Block 返回值的修飾抑淫,使用
_Nonnull
/_Nullable
,建議棄用__nonnull
/__nullable
。
為安全起見(jiàn)姥闪,Apple 制定的使用規(guī)則:
- 通過(guò) typedef 定義的類型的 nullability 特性通常依賴于上下文始苇,即使是在 Audited Regions 中,也不能假定它為
nonnull
筐喳; - 對(duì)于復(fù)雜的指針類型(如 id * )必須顯式去指定是
nonnull
還是nullable
催式。例如,指定一個(gè)指向nullable
對(duì)象的nonnull
指針避归,可以使用__nullable id * __nonnul
荣月; - 我們經(jīng)常使用的
NSError **
通常是被假定為一個(gè)指向nullable
NSError 對(duì)象的nullable
指針。
使用區(qū)別
共有三種用法:
-
nonnull
梳毙,nullable
,null_unspecified
-
_Nonnull
哺窄,_Nullable
,_Null_unspecified
账锹, -
__nonnull
萌业,__nullable
,__null_unspecified
奸柬。
這幾種用法在聲明時(shí)的位置區(qū)別:
- 單下劃線和雙下劃線的需要放在類型定義之后咽白。
- 無(wú)下劃線的需要放在類型定義之前。
使用示例:
聲明屬性
@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;
修飾方法參數(shù)
- (void)methodWithString1:(nullable NSString *)aString;
- (void)methodWithString2:(NSString *__nullable)aString;
- (void)methodWithString3:(NSString *_Nullable)aString;
例外情況
??雙指針類型對(duì)象鸟缕、Block 的返回值晶框、Block 的傳入?yún)?shù)等,不能用 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 修飾的是方法傳入?yún)?shù),表示傳入的 Block 可以為空番甩,而不是修飾 BlocK 返回值侵贵。
- (void)methodWithBlock2:(void (^ _Nullable)(void))block;
- (void)methodWithBlock3:(void (^ __nullable)(void))block;
Block 傳入?yún)?shù)
- (void)methodWithBlock21:(nullable id nonnull(^)(id nullable parameters))block;
// ?? 以上 nullable 修飾的是方法傳入?yún)?shù),表示傳入的 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>
// 在這兩個(gè)宏之間的代碼,所有簡(jiǎn)單指針對(duì)象都被假定為 nonnull 宴胧,因此我們只需要去指定那些 nullable 指針對(duì)象即可漱抓。
NS_ASSUME_NONNULL_BEGIN
@interface MyObject : NSObject
// 默認(rèn)為 nonnull
@property (nonatomic, copy) NSString *aString;
// 方法返回值默認(rèn)為 nonnull
// 方法的參數(shù)顯式聲明為 nullabel,表示入?yún)⒖梢詾榭?- (id)methodWithString:(nullable NSString *)string;
@end
NS_ASSUME_NONNULL_END