skip to Main Content

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

2

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search