我在使用textField
. 我已经复制了下面的相关代码(并在这里和那里删除了一些不应该影响我得到的错误的东西)。
- 代码构建并运行良好。
textFields
我可以在(电子邮件、密码)中输入文本。- 但是当我尝试在任何地方使用文本时,它会出错:
fatal error: attempt to bridge an implicitly unwrapped optional containing nil
在 Alamofire 代码上,特别是参数。 - 即使我只是尝试
print(email)
我得到一个零错误。
我在这里错过了什么吗?我觉得这应该很容易,但我似乎无法弄清楚。
class loginPageViewController: UIViewController, TextFieldDelegate {
private var email: TextField!
private var password: TextField!
func loginButton(sender: RaisedButton!){
let parameters = [
"email" : email,
"password" : password
]
Alamofire.request(.POST, "https://kidgenius.daycareiq.com/api/v1/sessions", parameters: parameters, encoding: .URL)
//other alamofire code not relevant to question
}
override func viewDidLoad() {
super.viewDidLoad()
prepareEmail()
preparePassword()
prepareLoginButton()
}
private func prepareEmail() {
let email : TextField = TextField(frame: 100, 100, 200, 45))
email.delegate = self
email.placeholder = "Email"
//I removed some non relevant code here, just styling stuff
view.addSubview(email)
}
private func preparePassword() {
let password : TextField = TextField(100, 200, 200, 25))
password.secureTextEntry = true
password.delegate = self
password.placeholder = "Password"
//some more removed styling code
view.addSubview(password)
}
private func prepareLoginButton() {
let loginButton : RaisedButton = RaisedButton(frame: 100, 250, 150 , 35))
loginButton.setTitle("Login", forState: .Normal)
loginButton.addTarget(self, action: "loginButton:", forControlEvents: UIControlEvents.TouchUpInside)
//removed some styling code here
view.addSubview(loginButton)
}
}