When I implement Parse I get "fatal error: unexpectedly found nil while unwrapping an Optional value"
When I implement Parse I get "fatal error: unexpectedly found nil while unwrapping an Optional value"
我正在尝试使用 Parse 来编辑个人资料,在我启动应用程序时输入代码后,我点击了我制作的编辑个人资料的按钮,我得到了这个:
fatal error: unexpectedly found nil while unwrapping an Optional value
导致编辑配置文件控制器的 Segue 打不开,应用程序崩溃了。当 Parse 代码未实现时,到视图控制器的 segue 就可以正常打开。
import UIKit
import Parse
class EditProfileViewController: UIViewController {
@IBOutlet weak var profilePictureImageView: UIImageView!
@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var repeatPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Load user details
let userFirstName = PFUser.currentUser()?.objectForKey("first_name") as! String
let userLastName = PFUser.currentUser()?.objectForKey("last_name") as!String
firstNameTextField.text = userFirstName
lastNameTextField.text = userLastName
if(PFUser.currentUser()?.objectForKey("profile_picture") != nil)
{
let userImageFile:PFFile = PFUser.currentUser()?.objectForKey("profile_picture") as! PFFile
userImageFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
self.profilePictureImageView.image = UIImage(data: imageData!)
})
}
let image = UIImage(named: "navbar.png")
self.navigationController!.navigationBar.setBackgroundImage(image,forBarMetrics: .Default)
var nav = self.navigationController?.navigationBar
nav?.tintColor = UIColor.whiteColor()
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]; self.navigationController!.navigationBar.titleTextAttributes = titleDict as [NSObject : AnyObject]
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func doneButtonTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func chooseProfileButtonTapped(sender: AnyObject) {
}
@IBAction func saveButtonTapped(sender: AnyObject) {
}
}
您需要查明到底是哪一行引发了错误。基本上,此错误意味着您尝试访问具有可选值的变量,但结果发现该变量为 nil!
你为什么不设置一些断点并查看你的任何变量(尤其是与 Parse 相关的变量)return nil?
编辑(只是在黑暗中拍摄)
根据我在您的代码中看到的情况,可能是您没有正确地将文本字段link编辑到您的界面生成器文件中。因此,由于您在访问它们之前没有初始化它们,它们将 return nil 并且应用程序将在此处崩溃:
firstNameTextField.text = userFirstName
lastNameTextField.text = userLastName
确保文本字段已 linked 到您的界面生成器文件,或者,如果您不确定如何做,只需检查是否确实如此,然后在上面的前面插入这两行个数:
//Initialize them before accessing them
UITextField* firstNameTextField = [[UITextField alloc] init];
UITextField* lastNameTextField = [[UITextField alloc] init];
//Now you can securely access them
firstNameTextField.text = userFirstName
lastNameTextField.text = userLastName
如果应用程序现在不再崩溃,您知道是这些文本字段,您需要将它们正确地 link 到您的 xib 文件
我正在尝试使用 Parse 来编辑个人资料,在我启动应用程序时输入代码后,我点击了我制作的编辑个人资料的按钮,我得到了这个:
fatal error: unexpectedly found nil while unwrapping an Optional value
导致编辑配置文件控制器的 Segue 打不开,应用程序崩溃了。当 Parse 代码未实现时,到视图控制器的 segue 就可以正常打开。
import UIKit
import Parse
class EditProfileViewController: UIViewController {
@IBOutlet weak var profilePictureImageView: UIImageView!
@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var repeatPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Load user details
let userFirstName = PFUser.currentUser()?.objectForKey("first_name") as! String
let userLastName = PFUser.currentUser()?.objectForKey("last_name") as!String
firstNameTextField.text = userFirstName
lastNameTextField.text = userLastName
if(PFUser.currentUser()?.objectForKey("profile_picture") != nil)
{
let userImageFile:PFFile = PFUser.currentUser()?.objectForKey("profile_picture") as! PFFile
userImageFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
self.profilePictureImageView.image = UIImage(data: imageData!)
})
}
let image = UIImage(named: "navbar.png")
self.navigationController!.navigationBar.setBackgroundImage(image,forBarMetrics: .Default)
var nav = self.navigationController?.navigationBar
nav?.tintColor = UIColor.whiteColor()
let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]; self.navigationController!.navigationBar.titleTextAttributes = titleDict as [NSObject : AnyObject]
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func doneButtonTapped(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func chooseProfileButtonTapped(sender: AnyObject) {
}
@IBAction func saveButtonTapped(sender: AnyObject) {
}
}
您需要查明到底是哪一行引发了错误。基本上,此错误意味着您尝试访问具有可选值的变量,但结果发现该变量为 nil!
你为什么不设置一些断点并查看你的任何变量(尤其是与 Parse 相关的变量)return nil?
编辑(只是在黑暗中拍摄)
根据我在您的代码中看到的情况,可能是您没有正确地将文本字段link编辑到您的界面生成器文件中。因此,由于您在访问它们之前没有初始化它们,它们将 return nil 并且应用程序将在此处崩溃:
firstNameTextField.text = userFirstName
lastNameTextField.text = userLastName
确保文本字段已 linked 到您的界面生成器文件,或者,如果您不确定如何做,只需检查是否确实如此,然后在上面的前面插入这两行个数:
//Initialize them before accessing them
UITextField* firstNameTextField = [[UITextField alloc] init];
UITextField* lastNameTextField = [[UITextField alloc] init];
//Now you can securely access them
firstNameTextField.text = userFirstName
lastNameTextField.text = userLastName
如果应用程序现在不再崩溃,您知道是这些文本字段,您需要将它们正确地 link 到您的 xib 文件