@IBAction func login(sender: UIButton) {
let parameters = [
"username": usernameTextField?.text,
"password": passwordTextField?.text
]
Alamofire.request(.POST, "http://192.168.1.107:8080/api/v1/user/login", parameters: parameters, encoding: .JSON)
.validate()
.responseJSON { response in
switch response.result {
case .Success(let JSON):
print("Validation Successful")
if (JSON["status"] as! String != "Success") {
self.popup("Invalid Account", message: "Please check your username and password and try again")
}
case .Failure(let error):
print(error)
}
}
}
当我将用户名和密码硬编码在字典中时,这些参数工作正常。但是当我将其更改为文本字段时,我收到错误消息 Cannot conversion value of type [String: String?] to [String: Anyobject]?
如何使参数为anyobject,以便它被接受?
最佳答案
错误消息显示参数中有可选类型。
如果您在运行时创建文本字段,只需在使用前解开值即可。 如果不在运行时创建,只需像
一样使用它let parameters = [
"username": usernameTextField.text,
"password": passwordTextField.text
]
关于swift - 来自文本字段的 Alamofire 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38519817/