我刚刚开始在Swift中实现我们的一些新功能而不是Objective-c,到目前为止一切正常,但令我困惑的一件事是如何正确使用Obj- C在斯威夫特块。在Swift中使用Objective-C块时保留参数类型
在我的OBJ-C级的我已经定义了一个块被用来处理呼叫我们的API HTTP响应:
typedef void(^CCAPIClientRequestCompletionBlock)(id response, NSArray *messages, NSDictionary *metaData, NSError *error);
这在下面的方法使用:
-(void)createMeetingWithUsers:(NSArray *)users subject:(NSString *)subject andDescription:(NSString *)description withCompletionBlock:(CCAPIClientRequestCompletionBlock)completionBlock;
我现在正在编写一个API客户端来访问Swift中的新API,并试图将该块作为闭包重用。下面的代码生成并运行:
apiClient.createMeeting(withUsers: userIds, subject: subject, andDescription: description) { (response, messages, metaData, error) -> Void in
}
,但我希望能够保持的参数类型,我想我应该可以做一些事情,如:
apiClient.createMeeting(withUsers: userIds, subject: subject, andDescription: description) { (response:Any?, messages:[Any], metaData:[AnyHashable:Any], error:NSError) -> Void in
}
但是当我尝试这一点,我收到一个错误:
Cannot convert value of type '(Any?, [Any], [AnyHashable : Any], NSError) -> Void' to expected argument type 'CCAPIClientRequestCompletionBlock!'
我在这里错过了什么?
您没有任何空性属性声明,但不知为何,你期望的响应是可选的。那可能怎么样? – Andy