2015-09-04 49 views
0

我想使用解析來編輯配置文件,並在我把代碼放入啓動應用程序時,我點擊了我所做的按鈕編輯個人資料,我得到這個:當我執行解析我得到「致命錯誤:意外地發現零,而解包可選值」

fatal error: unexpectedly found nil while unwrapping an Optional value

塞格我導致編輯配置文件控制器不打開,應用程序崩潰。當解析代碼沒有被執行時,視圖控制器的繼續打開就好了。

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) { 
    } 
} 
+0

你在哪一行得到t他的錯誤? –

+0

我不知道如何找到這個。我仍然在學習,但我很確定這是「讓userImageFile:PFFile = PFUser.currentUser()?. objectForKey(」profile_picture「)as!PFFile」 –

+0

可能的重複[什麼是致命錯誤:意外地發現無展開一個可選值「的意思?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jtbandes

回答

0

你需要找出哪一行確切地拋出錯誤。基本上,這個錯誤意味着你試圖訪問一個具有可選值的變量,但事實證明該變量是零!

爲什麼不設置一些斷點並查看是否有任何變量(特別是與Parse相關的變量)返回nil?

編輯(只是在黑暗中拍攝)

從我可以在你的代碼中看到的,這可能是因爲你沒有正確鏈接的文本框,以你的界面生成器文件。因此,既然你不是在訪問之前初始化他們,他們將返回nil和應用程序會崩潰的位置:

firstNameTextField.text = userFirstName 
lastNameTextField.text = userLastName 

確保文本框鏈接到你的界面生成器文件,或者,如果你不能確定如何要做到這一點,只是檢查,如果這確實是這樣的,並插入上述那些在這兩行:

//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 

如果應用程序現在已經不崩潰,你知道這是這些文本框,您需要正確地將它們鏈接到你的xib文件

相關問題