objective-c - 如何在基于 block 的 API 方法中使用非空和可为空的 Objec

考虑以下方法

- (void)methodWithArg:(NSString *)arg1 andArg:(NSString *)arg2 completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler;

使用新的 nonnullnullable annotation keywords我们可以如下丰富它:

- (void)methodWithArg:(nonnull NSString *)arg1 andArg:(nullable NSString *)arg2 completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler;

但我们也收到此警告:

Pointer is missing a nullability type specifier (__nonnull or __nullable)

它指的是第三个参数( block 一)。

documentation没有举例说明如何指定 block 参数的可空性。它逐字说明

You can use the non-underscored forms nullable and nonnull immediately after an open parenthesis, as long as the type is a simple object or block pointer.

我尝试将 block 的两个关键字之一(在任何位置)放在没有任何运气的情况下。还尝试了下划线前缀的变体(__nonnull__nullable)。

因此我的问题是:如何指定 block 参数的可空性语义?

最佳答案

这似乎有效

- (void)methodWithArg:(nonnull NSString *)arg1 
  andArg:(nullable NSString *)arg2 completionHandler:(nullable void (^)
  (NSArray * _Nullable results, NSError * _Nonnull error))completionHandler

您需要为 block 及其参数指定可空性...

编辑:有关详细信息,请参阅 Swift Blog

关于objective-c - 如何在基于 block 的 API 方法中使用非空和可为空的 Objective-C 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647919/

相关文章:

objective-c - 循环遍历 NSMutableDictionary

objective-c - 如何将 NSInteger 转换为 int?

objective-c - 如何在 NSString 中找到最后一次出现的子字符串?

objective-c - 如何在运行时使用 Objective-C 动态创建选择器?

ios - 文档 Main.storyboard 需要 Xcode 8.0 或更高版本

iphone - 向下移动 UITabBarItem 图像?

objective-c - NSString:isEqual 与 isEqualToString

objective-c - presentViewController : crash on iOS

objective-c - Xcode 版本 (CFBundleShortVersionString

ios - 如何检查 UILabel 是否被截断?