objective-c - Objective-C 中可为空、__nullable 和 _Nulla

在 Xcode 6.3 中引入了新的注解,以便更好地表达 API 在 Objective-C 中的意图(当然也是为了确保更好的 Swift 支持)。这些注解当然是 nonnullnullablenull_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 个不同的可空性注释:

  • nonnullnullablenull_unspecified
  • _Nonnull_Nullable_Null_unspecified
  • __nonnull__nullable__null_unspecified

尽管我知道为什么以及在哪里使用哪个注释,但我对应该使用哪种类型的注释、在哪里以及为什么使用感到有些困惑。这是我能收集到的:

  • 对于属性,我应该使用 nonnullnullablenull_unspecified
  • 对于方法参数,我应该使用 nonnullnullablenull_unspecified
  • 对于 C 方法,我应该使用 __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 the nonnull and returns_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/

相关文章:

objective-c - "FOUNDATION_EXPORT"与 "extern"

iphone - 从实例中获取类的名称

objective-c - Swift References 中的 _ 下划线代表什么?

ios - 使用 AVFoundation AVPlayer 循环播放视频?

iphone - 如何在 Objective-C 2.0 中将方法标记为已弃用?

ios - 以编程方式调用电话

objective-c - NSInvocation 傻瓜?

ios - 如何设置一个简单的委托(delegate)来在两个 View Controller 之间

ios - 较小时 UIScrollView 的中心内容

ios - iPhone SDK : what is the difference between