【发布时间】:2015-05-28 09:11:16
【问题描述】:
我正在使用用 Objective C 编写的第三方库,方法如下:
- (void)manageServerResponse:(NSURLResponse*)response NSData:(NSData*)data andNSError:(NSError*)error onComplete:(void (^)(NSInteger kindOfError, NSDictionary*jsonResponse))onComplete;
当我将它移植到 swift 时,我会执行以下操作:
typealias ResponseCompletedBlock = (NSInteger, NSDictionary?) -> Void
...
let completedResponseMethod : ResponseCompletedBlock = {(kindOfError: NSInteger, jsonResponse: NSDictionary?) -> Void in
self.onComplete(kindOfError, jsonResponse: jsonResponse)}
let responseManager: ResponseManager = ResponseManager.sharedResponseManager() as! ResponseManager
responseManager.manageServerResponse(response,
NSData: data,
andNSError: error,
onComplete: completedResponseMethod)
我收到此错误:
无法使用类型参数列表调用“manageServerResponse” '(NSURLResponse?, NSData: NSData?, andNSError: NSError?, onComplete: ResponseCompletedBlock)'
如果我将最后一句替换为
responseManager.manageServerResponse(response,
NSData: data,
andNSError: error,
onComplete: nil)
一切正常,所以我认为问题出在块结构上,但我尝试更改所有内容,但错误仍然存在。
你能帮忙吗?
【问题讨论】:
-
我还没有 Swift 的经验,但是你有没有尝试让
NSDictionary在你的(NSInteger, NSDictionary?) -> Void闭包中隐式展开? -
在 swift 块中,您通常需要明确地确定需要解包的内容。添加一个!或者 ?可能会解决问题。一般来说 !为我解决了...
-
我已经尝试了几件事,包括添加!和 ?到 NSDictionary 参数。没有运气
标签: objective-c swift objective-c-blocks