0

I am creating an app that uses Facebook to login and retrieves your profile picture to use as the profile picture for the app. This is the code:

if let user = FIRAuth.auth()?.currentUser {
  // User is signed in.
  let name = user.displayName
  let email = user.email
  let photoUrl = user.photoURL
  let uid = user.uid;

  self.profileName.text = name

  // ERROR: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
  let data = NSData(contentsOfURL: photoUrl!)
  self.profilePic.image = UIImage(data: data!)


}

This error only happens when using login NOT through Facebook. When doing it through Facebook it works fine. I have a hunch that the error arises because if I log in regularly (not through Facebook) this line will say "there is no photo, what are you talking about?" and crash. However, I am unsure as to how to circumvent this issue. I am using Firebase.

Any help is greatly appreciated.

evanhaus
  • 727
  • 3
  • 12
  • 30
  • 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) – Hamish Jul 20 '16 at 07:00

1 Answers1

1

This means that photoUrl is nil. The code crashes because you're using the forced unwrap operator !. You should safely unwrap it instead:

if let photoUrl = photoUrl, data = NSData(contentsOfURL: photoUrl) {
    self.profilePic.image = UIImage(data: data)
else {
    print("No photo")
}
Silvan Mosberger
  • 1,786
  • 14
  • 19