【发布时间】:2015-11-22 04:37:35
【问题描述】:
我正在尝试为应用程序实现登录服务,如果响应状态码不等于 200,我想抛出异常。我正在使用 Alamofire 作为 HTTP 网络库。
static func loginWithUsername(username: String, andPassword password: String) throws {
let action = "SmartShoppers/login"
let url = baseUrl + action
Alamofire.request(.POST, url, parameters: ["username": username, "password": password])
.response { request, response, data, error in
if response?.statusCode != 200 {
throw ServiceError.LogInError
}
}
}
这是 ServiceError 枚举定义:
enum ServiceError: ErrorType {
case LogInError
}
当它被抛出在按下按钮时调用的函数中时,我想要处理这个异常。此函数位于与包含该按钮的 ViewController 关联的 ViewController 类中。将要执行的代码是:
do {
try Service.loginWithUsername(username, andPassword: password)
} catch {
// SHOW AN ALERT MESSAGE
}
为什么我会收到消息Cannot call value of non-function type 'NSHTTPURLResponse'?这是实现此服务的正确方法吗?如果没有,请帮助我。
【问题讨论】:
标签: asynchronous swift2 alamofire