AndrewSB
AndrewSB

Reputation: 146

return callback in swift

I have a callback as one of the parameters of this function, on the callback(false) line it that Type 'Void' does not conform to protocol 'BooleanLiteralConvertible'. Why? My callback should return a Bool, which false definitely is

func facebookLoginWithCallback(callback: ((Void) -> (Bool))) -> Void {
    let permissions = ["public_profile", "email", "user_friends"]
    PFFacebookUtils.logInWithPermissions(permissions, block: {(user: PFUser?, error: NSError?) -> (Void) in
        if (user == nil) {
            println(error)
            callback(false)
        } else if ((user?.isNew) == true) {

        }
    })
}

Upvotes: 0

Views: 1409

Answers (2)

s Hetul
s Hetul

Reputation: 94

If you just want to return bool value, then use following code

    func facebookLoginWithCallback(callback: (Bool) -> Void) {
    let permissions = ["public_profile", "email", "user_friends"]
    PFFacebookUtils.logInWithPermissions(permissions, block: {(user: PFUser?, error: NSError?) -> (Void) in
        if (user == nil) {
            println(error)
            callback(false)
        } else if ((user?.isNew) == true) {

        }
    })
}

Upvotes: 1

Bryan Chen
Bryan Chen

Reputation: 46578

Yes, your callback does return a Bool, but it takes no arguments (i.e. Void).

You invoke it with argument false which can't be converted to Void hence the error message.

You can change the callback type to Bool -> Void so it takes a Bool and does not return value.

Upvotes: 2

Related Questions