JMIT
JMIT

Reputation: 179

PFUser not unwrapped - swift

I'm beginning to learn swift with parse and i've run into this error:

"Value of optional type 'PFUser?' not unwrapped; did you mean to use '!' or '?'

I can't seem to get it to work...

        PFFacebookUtils.logInWithPermissions(["public_profile",
        "user_about_me", "user_birthday"], block: {
            user, error in

            if user == nil {
                println("the user canceled fb login")
                //add uialert

                return

            }
                //new user
            else if user.isNew {
                println("user singed up through FB")

                //get information from fb then save to parse
                FBRequestConnection.startWithGraphPath("/me?fields=picture,first_name,birthday,gender",  completionHandler: {
                    connection, result, error in
                    //print results
                    println(result)
                    //result dictionary about user
                    var r = result as NSDictionary
                    //prnt dictionary
                    println(NSDictionary)
                    //match parse column with what fb sends
                    user["firstName"] = r["first_name"]
                    user["gender"] = r["gender"]
                    //r = result, then key into using picture.  Then key into url using the data
                 let pictureURL = ((r["picture"] as NSDictionary)["data"] as NSDictionary) ["url"] as String

Upvotes: 0

Views: 152

Answers (2)

egor.zhdan
egor.zhdan

Reputation: 4605

Here is explanation: What is an "unwrapped value" in Swift?

PFFacebookUtils.logInWithPermissions(["public_profile",
    "user_about_me", "user_birthday"], block: {
        user, error in

        if user == nil {
            println("the user canceled fb login")
            //add uialert

            return

        }
            //new user
        else if user!.isNew {
            println("user singed up through FB")

            //get information from fb then save to parse
            FBRequestConnection.startWithGraphPath("/me?fields=picture,first_name,birthday,gender",  completionHandler: {
                connection, result, error in
                //print results
                println(result)
                //result dictionary about user
                var r = result as NSDictionary
                //prnt dictionary
                println(NSDictionary)
                //match parse column with what fb sends
                user["firstName"] = r["first_name"]
                user["gender"] = r["gender"]
                //r = result, then key into using picture.  Then key into url using the data
             let pictureURL = ((r["picture"] as NSDictionary)["data"] as NSDictionary) ["url"] as String

Upvotes: 1

AdamPro13
AdamPro13

Reputation: 7400

Instead of using if user == nil {... you should really use

    if let user = user { 
       // Login succeeded...
    } 
    else { 
       // Login failed
    } 

The variable user will then be unwrapped inside the if let and you can continue using user the same way you are.

Upvotes: 2

Related Questions