Why can I declare a variable without writing an optional label?

First of all, I believe that my question is different from these questions about "what is optional in fast." Because I ask why I can do this, and not what it is.

I am new to speed. When I learn this fabulous language tonight, I had a problem when I never saw this mark - " ? " In a programming language. And I was looking for him for a while. I know what an optional binding is now.

But now I have a new question. When I want to declare a variable that is not optional. I can write this syntax:

var friend: String

But I can not write:

var friend: String = nil

To declare a nil variable, I can only use the optional parameter:

var friend: String? = nil

. friend, nil, ? . , , nil .

, , , . , , .

!

+4
2

Swift , . nil ( ) - . , , , if.

Swift , .

+7

- .

- , . , javascript, , . , , , java, NullPointerException .

. , , . , , , ? " ". , Swifts, . , , , Swifts .

enum Optional<T> {
    case None // has no value
    case Some(T) // has some value
} 
0

Source: https://habr.com/ru/post/1620845/


All Articles