0

I am trying to use Parse to edit profile and after I put the code in when I launch the app I clicked the button I made to edit profile and I get this:

fatal error: unexpectedly found nil while unwrapping an Optional value

The Segue I have leading to the edit profile controller does not open and the app crashes. When the Parse code is not implemented the segue to the view controller opens just fine.

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) {
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
John Harris
  • 109
  • 1
  • 1
  • 9
  • At which line you are getting this error? – Dharmesh Kheni Sep 04 '15 at 05:00
  • I am not sure how to find this out. I am still learning but I am pretty sure It is this "let userImageFile:PFFile = PFUser.currentUser()?.objectForKey("profile_picture") as! PFFile" – John Harris Sep 04 '15 at 05:13
  • possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jtbandes Sep 05 '15 at 06:16

1 Answers1

0

You need to find out which line exactly throws the error. Basically, this error means that you try to access a variable with optional value, but it turns out the variable is nil!

Why don't you set some break points and see if any of your variables (esp. the ones related to Parse) return nil?

EDIT (just a shot in the dark)

From what I can see in your code, it could be that you have not correctly linked the textfields to your interface builder file. Thus, since you are not initializing them before accessing them, they will return nil and the app will crash here:

firstNameTextField.text = userFirstName
lastNameTextField.text = userLastName

Make sure the textfields are linked to your interface builder file, or, if you are unsure about how to do it, just check if this is indeed the case and insert these two lines before the above ones:

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

In case the app now doesn't crash anymore, you know it's been these textfields and you need to properly link them to your xib file

eschanet
  • 1,063
  • 6
  • 15