swift 中的类型别名闭包错误
typealias closure error in swift
我在 Swift 项目中使用 AFNetworking。
1 - 为闭包输入别名:
typealias successBlock = (AFHTTPRequestOperation! ,AnyObject!)-> Void
typealias failureBlock = (AFHTTPRequestOperation, NSError!) -> Void
2 - 定义函数:
func getUserInfo(success: (successBlock)!, failure: (failureBlock)!) {
let path = "https://api.wei.s.json"
let parameters = ["source":"key"]
self.GET(path, parameters: parameters, success: success, failure: failure)
}
3 - 错误:
Cannot invoke 'GET' with an argument list of type '(String,parameters: [String : String], success: (successBlock)!, failure: (failureBlock)!)'
感谢您的帮助。
EIDT:
typealias failureBlock = (AFHTTPRequestOperation, NSError!) -> Void
至
typealias failureBlock = (AFHTTPRequestOperation!, NSError!) -> Void
试试看:
func getUserInfo(success: (successBlock)!, failure: (failureBlock)!) {
let path = "https://api.wei.s.json"
let parameters = ["source":"key"]
self.GET(path, parameters: parameters, success: success!, failure: failure!)
}
(注意参数成功和失败的两个 "bang"!
这是基于 GET 函数期望闭包而不是引用闭包的可选假设。可能就是这样。否则,使用击键获取 self.GET 上的方法完成并查看 Swift 期望的类型。它会告诉你哪里有问题。
我觉得你少了一个!在 failureBlock typealias 定义上,它期望一个显式解包的可选 AFHTTPRequestOperation 而不是标准的 AFHTTPRequestOperation,它们实际上是不同的类型。我认为应该是,
typealias failureBlock = (AFHTTPRequestOperation!, NSError!) -> Void
我在 Swift 项目中使用 AFNetworking。
1 - 为闭包输入别名:
typealias successBlock = (AFHTTPRequestOperation! ,AnyObject!)-> Void
typealias failureBlock = (AFHTTPRequestOperation, NSError!) -> Void
2 - 定义函数:
func getUserInfo(success: (successBlock)!, failure: (failureBlock)!) {
let path = "https://api.wei.s.json"
let parameters = ["source":"key"]
self.GET(path, parameters: parameters, success: success, failure: failure)
}
3 - 错误:
Cannot invoke 'GET' with an argument list of type '(String,parameters: [String : String], success: (successBlock)!, failure: (failureBlock)!)'
感谢您的帮助。
EIDT:
typealias failureBlock = (AFHTTPRequestOperation, NSError!) -> Void
至
typealias failureBlock = (AFHTTPRequestOperation!, NSError!) -> Void
试试看:
func getUserInfo(success: (successBlock)!, failure: (failureBlock)!) {
let path = "https://api.wei.s.json"
let parameters = ["source":"key"]
self.GET(path, parameters: parameters, success: success!, failure: failure!)
}
(注意参数成功和失败的两个 "bang"!
这是基于 GET 函数期望闭包而不是引用闭包的可选假设。可能就是这样。否则,使用击键获取 self.GET 上的方法完成并查看 Swift 期望的类型。它会告诉你哪里有问题。
我觉得你少了一个!在 failureBlock typealias 定义上,它期望一个显式解包的可选 AFHTTPRequestOperation 而不是标准的 AFHTTPRequestOperation,它们实际上是不同的类型。我认为应该是,
typealias failureBlock = (AFHTTPRequestOperation!, NSError!) -> Void