在 Xcode 6.3 中引入了新的注解,以便更好地表达 API 在 Objective-C 中的意图(当然也是为了确保更好的 Swift 支持)。这些注解当然是 nonnull
、nullable
和 null_unspecified
。
但是在 Xcode 7 中,出现了很多警告,例如:
Pointer is missing a nullability type specifier (_Nonnull, _Nullable or _Null_unspecified).
除此之外,Apple 使用另一种类型的可空性说明符,标记其 C 代码 (source):
CFArrayRef __nonnull CFArrayCreate(CFAllocatorRef __nullable allocator, const void * __nonnull * __nullable values, CFIndex numValues, const CFArrayCallBacks * __nullable callBacks);
所以,总而言之,我们现在有这 3 个不同的可空性注释:
nonnull
、nullable
、null_unspecified
_Nonnull
、_Nullable
、_Null_unspecified
__nonnull
、__nullable
、__null_unspecified
尽管我知道为什么以及在哪里使用哪个注释,但我对应该使用哪种类型的注释、在哪里以及为什么使用感到有些困惑。这是我能收集到的:
nonnull
、nullable
、null_unspecified
。nonnull
、nullable
、null_unspecified
。__nonnull
、__nullable
、__null_unspecified
。_Nonnull
、_Nullable
、_Null_unspecified
。但我仍然对为什么我们有这么多基本上做同样事情的注释感到困惑。
所以我的问题是:
这些注释之间的确切区别是什么,如何正确放置它们以及为什么?
最佳答案
来自clang
documentation :
The nullability (type) qualifiers express whether a value of a given pointer type can be null (the
_Nullable
qualifier), doesn’t have a defined meaning for null (the_Nonnull
qualifier), or for which the purpose of null is unclear (the_Null_unspecified
qualifier). Because nullability qualifiers are expressed within the type system, they are more general than thenonnull
andreturns_nonnull
attributes, allowing one to express (for example) a nullable pointer to an array of nonnull pointers. Nullability qualifiers are written to the right of the pointer to which they apply.
,和
In Objective-C, there is an alternate spelling for the nullability qualifiers that can be used in Objective-C methods and properties using context-sensitive, non-underscored keywords
所以对于方法返回和参数,您可以使用
双下划线版本 __nonnull
/__nullable
/__null_unspecified
代替单下划线版本,或代替非下划线版本。 区别在于单下划线和双下划线需要放在类型定义之后,非下划线需要放在类型定义之前。
因此,以下声明是等价且正确的:
- (nullable NSNumber *)result
- (NSNumber * __nullable)result
- (NSNumber * _Nullable)result
对于参数:
- (void)doSomethingWithString:(nullable NSString *)str
- (void)doSomethingWithString:(NSString * _Nullable)str
- (void)doSomethingWithString:(NSString * __nullable)str
对于属性:
@property(nullable) NSNumber *status
@property NSNumber *__nullable status
@property NSNumber * _Nullable status
然而,当涉及到双指针或返回不同于 void 的 block 时,事情会变得复杂,因为这里不允许使用非下划线:
- (void)compute:(NSError * _Nullable * _Nullable)error
- (void)compute:(NSError * __nullable * _Null_unspecified)error;
// and all other combinations
与接受 block 作为参数的方法类似,请注意 nonnull
/nullable
限定符适用于 block ,而不是它的返回类型,因此以下是等价的:
- (void)executeWithCompletion:(nullable void (^)())handler
- (void)executeWithCompletion:(void (^ _Nullable)())handler
- (void)executeWithCompletion:(void (^ __nullable)())handler
如果 block 有返回值,那么你将被迫进入下划线版本之一:
- (void)convertObject:(nullable id __nonnull (^)(nullable id obj))handler
- (void)convertObject:(id __nonnull (^ _Nullable)())handler
- (void)convertObject:(id _Nonnull (^ __nullable)())handler
// the method accepts a nullable block that returns a nonnull value
// there are some more combinations here, you get the idea
作为结论,您可以使用任何一个,只要编译器可以确定要分配限定符的项目。
关于objective-c - Objective-C 中可为空、__nullable 和 _Nullable 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32452889/