Home > Mobile >  Variable binding in a condition requires an initializer?
Variable binding in a condition requires an initializer?

Time:08-31

in the Swift Playground I have: "You can use a shorter spelling to unwrap a value, using the same name for that unwrapped value." then,

   if let nickname {
    print("Hey, \(nickname)")
}

I receive "Variable binding in a condition requires an initializer" when I try to run it

CodePudding user response:

Upgrade to Xcode 14.

In Xcode 14, you can write this:

var nickname : String? = "Nick"

if let nickname {
    print("Hey \(nickname)")
}

and it'll work fine because you're using Swift 5.7. Xcode 13.x and earlier have older Swift versions which don't work with the abbreviated unwrapping form that you're trying to use.

CodePudding user response:

I think you should use if let n = nickname { Print("\(n)")

  • Related