我在我的应用中定义了这个类来处理对后端的请求
class BackendService {
// Retrieves user chat channels
class func getChatChannels(success:(response: Response)->(), failure: (response:Response)->()) {
let chatsURL = baseURL + "/chats"
performRequestWith(success, failure: failure, url: chatsURL, parameters: nil)
}
func performRequestWith(success:(response: Response)->(), failure: (response: Response) -> (), url: String, parameters: String?) {
var manager = Alamofire.Manager.sharedInstance
let keychain = Keychain(service:"com.domain.app")
let token = keychain.get("token")
if let token = token {
manager.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Bearer \(token)"]
manager.request(.GET, url, parameters:nil).responseJSON { (req, res, json, err) in
if(err != nil) {
var response = Response()
response.error = err
failure(response: response)
} else {
var response = Response()
if let httpStatus = HTTPStatus(rawValue: res!.statusCode) {
response.httpStatus = httpStatus
}
response.payload = JSON(json!)
success(response: response)
}
}
}
}
}
我正在尝试将getChatChannels
收到的回调/闭包参数传递给performRequestWith
。在performRequestWith(success, failure: failure, url: chatsURL, parameters: nil)
我得到了Extra argument 'failure' in call
我对Swift没什么经验,我显然在这里做了一些非常错误的事情。一些帮助将非常感激。
答案 0 :(得分:1)
您的方法调用代码没有问题。 问题是您是从类方法调用实例方法。
您应该将两种方法都更改为类方法,例如:
class func getChatChannels(success:(response: Response)->(), failure: (response:Response)->())
{
let chatsURL = baseURL + "/chats"
performRequestWith(success, failure: failure, url: chatsURL, parameters: nil)
}
class func performRequestWith(success:(response: Response)->(), failure: (response: Response) -> (), url: String, parameters: String?)
{
// Your code
}
或将两者都更改为实例方法,例如:
func getChatChannels(success:(response: Response)->(), failure: (response:Response)->())
{
let chatsURL = baseURL + "/chats"
performRequestWith(success, failure: failure, url: chatsURL, parameters: nil)
}
func performRequestWith(success:(response: Response)->(), failure: (response: Response) -> (), url: String, parameters: String?)
{
// Your code
}
另一种选择是在类方法中创建类的实例,并使用该调用实例方法。