Ayaan Grover
Ayaan Grover

Reputation: 11

Firebase login setup failure: "Initializer for conditional binding must have Optional type, not 'String'"

When I run my code I receive this error:

/Users/ayaangrover/Documents/Github/waffle/Waffle/Waffle/ContentView.swift:46:19 Initializer for conditional binding must have Optional type, not 'String'.

The edited code is here (the error occurs with accessToken?):

guard let user = result?.user,
      let idToken = user.idToken?.tokenString,
      let accessToken = user.accessToken?.tokenString else {
    print("Error retrieving tokens")
    return
}

and the full code is available at https://github.com/ayaangrover/waffle if you need it.

I tried to fix it by removing the ? after accessToken, but then I receive the error:

/Users/ayaangrover/Documents/Github/waffle/Waffle/Waffle/ContentView.swift:46:53 Cannot use optional chaining on non-optional value of type 'GIDToken'

Upvotes: 1

Views: 58

Answers (1)

soundflix
soundflix

Reputation: 2743

You are on the right track with removing the optional chaining ?.
But you have to also consider that the accessToken declaration is part of the conditional binding, which has 3 parts in your case, and all 3 values need to be Optional.
Move the accessToken declaration out of the conditional binding. So your code would look like this:

guard let user = result?.user,
      let idToken = user.idToken?.tokenString else {
    print("Error retrieving tokens")
    return
}
let accessToken = user.accessToken.tokenString

Upvotes: 0

Related Questions