Swift:尝试将元组传递给回调函数时得到 "ambiguous expression"
Swift: Getting "ambiguous expression" when try to pass tuple to callback function
我有这个 class 用于根据我的后端对用户进行身份验证。
class BackendService {
class func performLogin(#email: String, password: String, success:((res: NSHTTPURLResponse, json: JSON, statusCode: HTTPStatus))->(), failure: (NSError)->()) {
let loginURL = baseURL + "/login"
let parameters = ["email": email, "password": password]
Alamofire.request(.POST, loginURL, parameters: parameters).responseJSON { (req, res, json, err) in
if(err != nil) {
let response = (error: err!)
failure(response)
}
else {
if let httpStatus = HTTPStatus(rawValue: res!.statusCode) {
let response = (res: res, json: JSON(json!) , statusCode: httpStatus)
success(response)
}
}
}
}
在 success(response)
我得到 Type of expression is ambiguous without more context
。有任何想法吗?
有没有更好、更快捷的方式来编写这个class?
它没有编译的原因是你在成功元组声明周围有一组额外的括号。如果你删除它们,那么它将编译。这是您的函数的更新版本,其中包含尽可能少的编译更改。
You need to make sure you have baseURL
defined somewhere.
class func performLogin(#email: String, password: String, success:(res: NSHTTPURLResponse, json: JSON, statusCode: HTTPStatus)->(), failure: (NSError)->()) {
let loginURL = baseURL + "/login"
let parameters = ["email": email, "password": password]
Alamofire.request(.POST, loginURL, parameters: parameters).responseJSON { (req, res, json, err) in
if (err != nil) {
let response = (error: err!)
failure(response)
}
else {
if let httpStatus = HTTPStatus(rawValue: res!.statusCode) {
let response = (res: res!, json: JSON(json!), statusCode: httpStatus)
success(response)
}
}
}
}
这是您的原始函数的更新版本,它当然更干净,但还不完全安全。一个好的经验法则是感叹号越多,风险越大。
typealias LoginSuccessHandler = (NSHTTPURLResponse, JSON, HTTPStatus) -> Void
typealias LoginFailureHandler = (NSError) -> Void
class func performLogin(#email: String, password: String, success: LoginSuccessHandler?, failure: LoginFailureHandler?) {
let loginURL = baseURL + "/login"
let parameters = ["email": email, "password": password]
Alamofire.request(.POST, loginURL, parameters: parameters).responseJSON { request, response, json, error in
if let error = error {
failure?(error)
} else {
if let httpStatus = HTTPStatus(rawValue: response!.statusCode) {
success?(response!, JSON(json!), httpStatus)
}
}
}
}
您真的应该看看 Alamofire.Request
class 中内置的 @mattt 令人敬畏的验证逻辑。这样您就可以完全消除对 HTTPStatus
枚举的需要。
public func validate() -> Self
public func validate(contentType array: [String]) -> Self
public func validate(statusCode array: [Int]) -> Self
public func validate(statusCode range: Range<Int>) -> Self
public func validate(validation: Validation) -> Self
我有这个 class 用于根据我的后端对用户进行身份验证。
class BackendService {
class func performLogin(#email: String, password: String, success:((res: NSHTTPURLResponse, json: JSON, statusCode: HTTPStatus))->(), failure: (NSError)->()) {
let loginURL = baseURL + "/login"
let parameters = ["email": email, "password": password]
Alamofire.request(.POST, loginURL, parameters: parameters).responseJSON { (req, res, json, err) in
if(err != nil) {
let response = (error: err!)
failure(response)
}
else {
if let httpStatus = HTTPStatus(rawValue: res!.statusCode) {
let response = (res: res, json: JSON(json!) , statusCode: httpStatus)
success(response)
}
}
}
}
在 success(response)
我得到 Type of expression is ambiguous without more context
。有任何想法吗?
有没有更好、更快捷的方式来编写这个class?
它没有编译的原因是你在成功元组声明周围有一组额外的括号。如果你删除它们,那么它将编译。这是您的函数的更新版本,其中包含尽可能少的编译更改。
You need to make sure you have
baseURL
defined somewhere.
class func performLogin(#email: String, password: String, success:(res: NSHTTPURLResponse, json: JSON, statusCode: HTTPStatus)->(), failure: (NSError)->()) {
let loginURL = baseURL + "/login"
let parameters = ["email": email, "password": password]
Alamofire.request(.POST, loginURL, parameters: parameters).responseJSON { (req, res, json, err) in
if (err != nil) {
let response = (error: err!)
failure(response)
}
else {
if let httpStatus = HTTPStatus(rawValue: res!.statusCode) {
let response = (res: res!, json: JSON(json!), statusCode: httpStatus)
success(response)
}
}
}
}
这是您的原始函数的更新版本,它当然更干净,但还不完全安全。一个好的经验法则是感叹号越多,风险越大。
typealias LoginSuccessHandler = (NSHTTPURLResponse, JSON, HTTPStatus) -> Void
typealias LoginFailureHandler = (NSError) -> Void
class func performLogin(#email: String, password: String, success: LoginSuccessHandler?, failure: LoginFailureHandler?) {
let loginURL = baseURL + "/login"
let parameters = ["email": email, "password": password]
Alamofire.request(.POST, loginURL, parameters: parameters).responseJSON { request, response, json, error in
if let error = error {
failure?(error)
} else {
if let httpStatus = HTTPStatus(rawValue: response!.statusCode) {
success?(response!, JSON(json!), httpStatus)
}
}
}
}
您真的应该看看 Alamofire.Request
class 中内置的 @mattt 令人敬畏的验证逻辑。这样您就可以完全消除对 HTTPStatus
枚举的需要。
public func validate() -> Self
public func validate(contentType array: [String]) -> Self
public func validate(statusCode array: [Int]) -> Self
public func validate(statusCode range: Range<Int>) -> Self
public func validate(validation: Validation) -> Self