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:
var optionalString: String? = "Hello"var optionalString: String? = "Hello"- is it written what we put?to note that this assignment is optionalWrite 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? = "" ?
- Now here is the condition
if let name = optionalName. I see it this way, thelet namevariable is created and ifoptionalNamenot equal tonil(I understand that this is the same as in javanull), 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?
- And the last "Hello, (name)", I understand that this is the syntax
\()we use when we want to convert, let's say anintintoString
But why do we use it in this example, when we obviously have everything in the Sring ?