Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

fatal error: unexpectedly found nil while unwrapping an Optional value

// // ViewController.swift

import UIKit import Foundation

class ViewController: UIViewController, FBLoginViewDelegate { //iboutlet

@IBOutlet var fbLoginView : FBLoginView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.fbLoginView.delegate = self
    self.fbLoginView.readPermissions = ["public_profile", "email", "user_friends"]


}
var userlogged: FBGraphUser?=nil


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    println("seg")
    if segue.identifier == "mySegueId" {
        let vc: view_profile = segue.destinationViewController as view_profile


        vc.setprofileId(userlogged!)


    }
}

func loginViewShowingLoggedInUser(loginView : FBLoginView!) {
    println("User Logged In")
    println("This is where you perform a segue (and or another view).")


}



func loginViewFetchedUserInfo(loginView : FBLoginView!, user: FBGraphUser){
    userlogged=user




}





func loginViewShowingLoggedOutUser(loginView : FBLoginView!) {


    println("User Logged Out")
}

func loginView(loginView : FBLoginView!, handleError:NSError) {
    println("Error: \(handleError.localizedDescription)")
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

// // view_profile.swift

import Foundation import UIKit

class view_profile: UIViewController {

@IBOutlet weak var fbprofilepic: FBProfilePictureView!

override func viewDidLoad() {
    super.viewDidLoad()



}
func setprofileId(var userlogged: FBGraphUser){


    print("userlogged : ")
    println(userlogged.objectID)


    fbprofilepic.profileID=userlogged.objectID

    //ERROR thrown here BAD INSTRUCT.....

    print("fbprofilepic.profileID : ")
    println(fbprofilepic.profileID);


}

}

IDE output:

User Logged In This is where you perform a segue (and or another view). seg userlogged : 13352598658324870 fatal error: unexpectedly found nil while unwrapping an Optional value

^bump any suggestions?

1 Answer

This basically means that Swift is expecting a value returned but it's returning a nil. Do some quick code to test for nils

if (error != nil) {
    println("nil was detected!")
}else {
    println("No nil detected!")
}