我在使用textField
中的文字时遇到了一些麻烦。我已经复制了下面的相关代码(并删除了一些不应该影响我所遇到的错误的内容)。
textFields
(电子邮件,密码)中输入文字。fatal error: attempt to bridge an implicitly unwrapped optional containing nil
,特别是参数。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)
}
}
答案 0 :(得分:1)
问题是您在顶部
声明了email
变量
private var email: TextField!
然后您还要再次在函数email
内声明prepareEmail()
let email : TextField = TextField(frame: 100, 100, 200, 45))
但是你的private var email: TextField!
永远不会被实例化
要解决您的问题,请在let
prepareEmail()