简体   繁体   English

Swift:使用与Objective-C块兼容的闭包

[英]Swift : Use closure compatible with Objective-C block

I tried to define a closure using the following Objective-C blocks: 我尝试使用以下Objective-C块定义闭包:

typedef void(^UPBaseEventAPIArrayCompletion)(NSArray *results, UPURLResponse *response, NSError *error);

+ (void)getFriendsWithCompletion:(UPBaseEventAPIArrayCompletion)completion;

in this: 在此:

UPUserAPI.getFriendsWithCompletion({(friends: [AnyObject], response: UPURLResponse, error: NSErrorPointer) -> Void in

    })

... and got the following error (see image): ...并收到以下错误(参见图片):

Cannot convert value of type '([AnyObject], UPURLResponse, NSErrorPointer) -> Void' to expected argument type 'UPBaseEventAPIArrayCompletion!' 无法将类型'([[AnyObject],UPURLResponse,NSErrorPointer)-> Void'的值转换为预期的参数类型'UPBaseEventAPIArrayCompletion!'。

在此处输入图片说明

What's the correct way to do this? 正确的方法是什么?

Try this. 尝试这个。

UPUserAPI.getFriendsWithCompletion({(friends: [AnyObject]?, response: UPURLResponse?, error: NSError?) -> Void in
    //code 
})

In the Objective C code, any of those parameters could be nil. 在目标C代码中,任何这些参数都可以为nil。 So in Swift, these parameters have to be defined as Optionals. 因此在Swift中,这些参数必须定义为Optionals。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM