简体   繁体   中英

variable binding in a condition requires an initializer Swift GuideTour PlayGround error

I'm learning Swift, using this guide from the official website, in the form of PlayGround.

And I had this error: 在此处输入图像描述

I'm so confused... for one thing, this is an introduction guide, for another, I tried all of these and none of them work: 在此处输入图像描述 在此处输入图像描述

Luckily I finally got this one to work, but I don't know why... 在此处输入图像描述

So, I think my question is:

  1. Why there's an error?
  2. What's the original thought behind this part of guide design? I mean, what do they want to show us originally, knowing it's incorrect...
  3. In my working code case, I don't see I can change my code to what's showing in the original guide example of if condition using optional variable. There's no way for this code:
if let nickname {
    print("Hey, \(nickname)")
}

to work, right?

Answering your questions:

  1. There's an error because the Xcode version that you are working with has a Swift version <5.7 (Xcode <14) which does not yet support if let shorthand syntax for optional binding when shadowing an existing optional variable (proposed in SE-0345 ).

    For your second image, you are attempting to use optional binding with an non-optional string literal "something" , which is, in semantics, pointless, not to mention being syntactically invalid.

  2. The original idea behind this part of the guide is to familiarize you with the idea of dealing with optionals, specifically "optional binding", which is a technique used to work with optional values. You might wanna look into that via the official Swift language guide .

    Short story: Think of variables as boxes. In Swift, these boxes can contain a certain type of data (eg String , Int , Bool , etc.). All these types that you might be already familiar with are solid data types that guarantee something of that type will be present in the box. But in some cases, you will be exposed to (or need) uncertainties in which you have no idea whether or not a solid piece of data is present within the box. This is when optional values come in--boxes that do not guarantee the presence of values (this box can either have something solid inside or nothing at all, nil ).

    Usually, we can force unwrap the optional values, but that can cause an error in your program when the box has nothing inside (Swift be like: hey! you promised me that there's gonna be a value inside the box but there's nothing inside, you untruthful creature! what do I do now? *rage halt*). That's why you need an optional binding: you tell Swift to carefully unwrap it and do something with the potential value inside (if it can be safely unwrapped) and do something else when there's nothing inside. The conventional syntax is:

     if let aValue = anOptionalValue { // if there's something inside the box, do something with it print("haha found ya", aValue) } else { // otherwise do something else print("oh no! empty box...") }

    Swift will now be happy: "at least you warned me that it might be empty inside and told me exactly how to handle it."

  3. You are trying to use the shorthand syntax for optional binding which is only available after Swift 5.7 + Xcode 14. You'll need to get a Xcode 14 beta to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM