首页 > 解决方案 > Objective C 块语法 - Xcode 自动完成功能不起作用

问题描述

请提供一些 OBJECTIVE-C 块语法帮助。

这是我的呼叫站点 (AppDelegate) - 由 XCode 自动完成

Objective-C

  [SwiftClass passValue: response completion:^(NSDictionary<NSString *,NSString *> * _Nullable, ErrorResponse * _Nullable) { }];

我的方法看起来像这样(Swift)

  @objc static func passValue(code: ValidatedResponse, completion: @escaping ([String : String]?, ErrorResponse?) -> Void)

我收到一条错误消息: Parameter name omitted

所以,我给这样的参数命名:

[SwiftClass passValue: response completion:^(NSDictionary<NSString *,NSString *> *responseDictionary _Nullable, ErrorResponse *errorResponse _Nullable) {}];

现在我得到这个错误:

Incompatible block pointer types sending 'void (^)(NSDictionary<NSString *,NSString *> *__strong)' to parameter of type 'void (^ _Nonnull)(NSDictionary<NSString *,NSString *> * _Nullable __strong, ErrorResponse * _Nullable __strong)'

你们知道我在 ObjectiveC 中调用方法的方式有什么问题吗?

顺便说一句,是的,我知道文档和http://fuckingblocksyntax.com/,我已经看过那里并且无法找出问题所在。

标签: objective-cswiftxcodeobjective-c-blocks

解决方案


我找到了!参数名称必须在 Nullable 之后,如下所示:

[SwiftClass passValue: response completion:^(NSDictionary<NSString *,NSString *> * _Nullable responseDictionary, ErrorResponse * _Nullable errorResponse) {}];

推荐阅读