I started reading the Swift documentation and here is the first question.

Here is the code

var optionalString: String? = "Hello" print(optionalString == nil) var optionalName: String? = "John Appleseed" var greeting = "Hello!" if let name = optionalName { greeting = "Hello, \(name)" } 

And here are the questions:

  1. var optionalString: String? = "Hello" var optionalString: String? = "Hello" - is it written what we put ? to note that this assignment is optional

    Write a question mark (?).

What does it mean not mandatory? I mean, if I want to leave this value empty, like this var optionalString: String? = "" var optionalString: String? = "" ?

  1. Now here is the condition if let name = optionalName . I see it this way, the let name variable is created and if optionalName not equal to nil (I understand that this is the same as in java null ), then the condition works.

But then if this is a nil test, why couldn't it be written, something like if optionalName != nil ? Why create an extra variable?

  1. And the last "Hello, (name)", I understand that this is the syntax \() we use when we want to convert, let's say an int into String

But why do we use it in this example, when we obviously have everything in the Sring ?

    1 answer 1

    Well, I'll try:

    one) '?' This means that this variable may not be non-unitalized, in other words it does not contain a value. That is, can we write a var optionalString: String? and then when referring to it will return nil. on this topic, look here

    2) This is called optional binding, and is widely used in swift for cases where a variable can be nil. Your remark about if optionalName != nil is quite appropriate, just the creators of swift decided that it is more convenient to use the expanded constant than to expand optional each time in if-else (remember that if you can do if var). For example, let's say you have an optional variable, and you want to display its value

     var hello:String? = "123" if(hello != nil) { print(hello!) // обратите внимание на '!' - без него результат будет "Optional(123)" } else { print("empty") } if let const = hello { print(const) } else { print("empty") } 

    3) the \(varName) used to use variables / constants when creating a string, and so on, that is,

     let name = "Max" print ("hello \(name)") // напечатает "Hello Max" 

    for comparison in objC it would look like this

     NSString *name = @"Max"; NSLog(@"Hello %@", name); 

    another example:

     let digit = 1 let anotherDigit = 2 print("sum \(digit+anotherDigit)") // напечетает "sum 3" 
    • 1) A question about this syn var hello:String? = "123" syntax var hello:String? = "123" var hello:String? = "123" Do I need to specify ? if we still explicitly assign a value right away? 2) On question number 3, my question was that it is not entirely clear why I should specify \() if everything is clearly in a string? For comparison, in java sout("Hello" + String.valeuOf(34)); so here we String.valeuOf(34) in order to cast the int type into the String , but if we passed the string right away, it would not be necessary to bring it like so sout("Hello" + "34"); But in the swift, I understand it does not matter what type we always put \() , right? - Aleksey Timoshchenko
    • 1) in your particular situation '?' there is no need. In a situation where you, for example, create an instance variable, and do not assign a value to it in an instance, you will get an error when compiling. in this case, putting '?' you tell the compiler that you are satisfied that it is not initialized, and you assign it a value later. 2) I don’t know how to explain this in another way, in short, the "\" is an escape character, that is, what comes after it must be interpreted. that is, print("\(2+3)") will print "5" and not "(2 + 3)" - Max Mikheyenko
    • )) Okay, I'll ask differently) So you can write let name = "Max" print ("hello " + name) . ? The compiler will understand that the name also a String ? Sring compiler simply Sring String to Sring ? - Aleksey Timoshchenko
    • yes you can. Escape in my opinion is just easier to read "next year \(name) will be \(age+1)" or "next year " + name + "will be " + String.valueOf(age+1)" - Max Mikheyenko