Objective-C中 nullable、__nullable、_Nullable的区别

2022年4月27日 1406点热度 0人点赞 0条评论

Objective-C中 nullable、__nullable、_Nullable的区别

介绍:C系列对于nullable的解释: nullable C

渊源:在Xcode6.3中使用了双下划线的版本,但是由于和第三方库的潜在冲突,所以苹果在Xcode 7 中修改为单下划线+首字母大写;后续为了和旧版本Xcode兼容,继续使用双下划线的版本,也就是两种是等价的。 Apple Swift 博客

区别:区别在于单下划线和双下划线需要放在类型定义之后,而非下划线的需要放在类型定义前。

nonnull,nullable,null_unspecified
_Nonnull,_Nullable,_Null_unspecified
__nonnull,__nullable,__null_unspecified

 

这三种是等价的:

- (nullable NSNumber *)result
- (NSNumber * __nullable)result
- (NSNumber * _Nullable)

 

对于属性来说:

@property(nullable) NSNumber *status
@property NSNumber *__nullable status
@property NSNumber * _Nullable status

 

对于方法来说:

- (void)doSomethingWithString:(nullable NSString *)str
- (void)doSomethingWithString:(NSString * _Nullable)str
- (void)doSomethingWithString:(NSString * __nullable)str

 

必须使用下划线的情况:

- (void)compute:(NSError * _Nullable * _Nullable)error
- (void)compute:(NSError * __nullable * _Null_unspecified)error;

 

无返回值情况:

- (void)executeWithCompletion:(nullable void (^)())handler
- (void)executeWithCompletion:(void (^ _Nullable)())handler
- (void)executeWithCompletion:(void (^ __nullable)())handler

 

有返回值情况:

- (void)convertObject:(nullable id __nonnull (^)(nullable id obj))handler
- (void)convertObject:(id __nonnull (^ _Nullable)())handler
- (void)convertObject:(id _Nonnull (^ __nullable)())handler

 

 

 


数据参考/拷贝来源:https://stackoverflow.com/questions/32452889/difference-between-nullable-nullable-and-nullable-in-objective-c

数据快照:Difference between nullable, __nullable and _Nullable in Objective-C

帮助教程

提供最新的帮助教程,方便使用。

文章评论