Swift.Bool isn't a Bool?

Viewed 57

I had a function

func login<Bool> (parameters: [(String, Any)],
                  completion: @escaping (Bool) -> Void) {
    //Do something
}

And whenever I called the completion handler like so

completion(false)

or

completion(true)

XCode kept telling me: "Cannot convert value of type 'Swift.Bool' to expected argument type 'Bool'"

Eventually, I removed the at the beginning of the function, and the error went away. I thought that was declaring the type for the function, but I'm frankly not sure what that means, or what <> does despite googling. I'm sure it's explained well, but I'm not grasping the concept from googling alone. And why is Swift.Bool not able to be converted to Bool. I was able to find that there is an objective C version of bool and I thought maybe it was asking for that, but that didn't work either. What does that mean a Bool is?

1 Answers

The syntax func login<Bool> defines a type parameter called Bool. It has nothing to do with Swift.Bool. It's just a name you've defined. It's the same as if you declared a local variable var Bool: String. You'd have a variable named Bool that is actually a String. Don't do that.

If you want the value to always be Bool, remove the type parameter. This isn't generic.

func login(parameters: [(String, Any)],
           completion: @escaping (Bool) -> Void) {

For details on the <...> syntax and generics see Generics in The Swift Programming Language.

Related