-1

Hi I want users to be able to Register even if they don't choose a profile picture

Right now I use this code for the profile picture:

   let profileImageData = UIImageJPEGRepresentation((userImage.image!), 1)

    if (profileImageData != nil) {

        let profileImageFile = PFFile (data: profileImageData!)
        myUser.setObject(profileImageFile!, forKey: "profile_picture")
    }

And each time a user don't choose a profile picture while registering I get this error

fatal error: unexpectedly found nil while unwrapping an Optional value

I want the user to be able to register even if the value is nil.

Thank you very much

Kerby Jean
  • 207
  • 2
  • 12
  • Hi go through given stack overflow answer link. It will help you. http://stackoverflow.com/questions/25521886/how-to-check-if-imageview-is-not-nil-in-swift – korat prashant Jan 13 '16 at 05:55

2 Answers2

0

I am modifying your code here:

if let profileImageData = UIImageJPEGRepresentation((userImage.image!), 1){

    let profileImageFile = PFFile (data: profileImageData!)
    myUser.setObject(profileImageFile!, forKey: "profile_picture")

}else{
    print("No image selected")
}

Hope this helps!

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
-1

The problem is that you are trying to force unwrap the userImage.image variable even when its not set. You should force unwrap only when you are sure that the variable has a value.

The below modification should work

if let profileImage = userImage.image{
    let profileImageData = UIImageJPEGRepresentation((profileImage), 1)
    let profileImageFile = PFFile (data: profileImageData!)
    myUser.setObject(profileImageFile!, forKey: "profile_picture")
}else{
    //! Handle part when no image is selected
}