15

First of all, I believe my question is different with those questions about "what is optional in swift". Because I am asking why I can do this, not what is this.

I am new to swift. When I learn this fabulous language tonight, I got a problem that I never saw this mark-"?" in a programming language. And I searched it for a while. I know what optional binding is now.

But now I got 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't write:

var friend: String = nil

To declare a variable that is nil I can only use optional:

var friend: String? = nil

Let's see the first piece of code. When the new variable friend just be declared, its value is nil, right? Because I didn't assign any value to it. But according to the definition of optional, like the second and the third pieces of code, we can't assign nil to non-optional variables.

So my question is why I can declare a variable without optional mark but has no initial value. This question may be simple, but I really don't know why it happens.

Thanks in advance!

JW.ZG
  • 611
  • 1
  • 7
  • 20
  • 2
    The first one is not nil. It is an uninitialized variable. Like Marc said you will get a compile time error if you try to run the first one. However if it was an optional. It will allow you to run it, since it has a value of nil. – mn1 Dec 19 '15 at 05:13

2 Answers2

17

Swift allows you to declare a non-optional variable or constant without initializing it in the declaration, but you will have to assign it a value before using it. The value of the variable or constant is not nil (non-optionals can never be nil)--it simply has no defined value. Basically you are saying you will give it a value later, possibly based on the result of a computation or an if statement.

Swift will give you a compile-time error if you try to use a non-optional variable or constant without first assigning a value to it.

Marc Khadpe
  • 2,012
  • 16
  • 14
  • Thank you very much! I didn't know that before. I thought it is like C++, when I declare a new variable without initializing it, it is null (which in C++ is 0). Thanks again. – JW.ZG Dec 19 '15 at 05:51
  • Glad it helped! Note that optionals are automatically initialized to nil in Swift if you don't give them a value. Also, note that in C++ only _some_ types of variables (like global variables) are automatically initialized, while others (such as local variables) are left in an undefined state. See [this question for a rundown](http://stackoverflow.com/questions/2218254/variable-initialization-in-c). – Marc Khadpe Dec 19 '15 at 06:05
0

The first value you set is an uninitialized variable with the type of string.

A very common pattern in programming is to have an operation that might or might not return a value. In some languages, like javascript, when coming across a null value our program continues to operate just fine as long as our program is not dependent on that value to continue. In other languages, like java for example, a null value can cause a NullPointerException and crash our program.

Swift's solution to this problem is optionals. By default if we define a variable without a value, the compiler will complain. So, in order to declare a value that may or may not have a value at runtime, we need to add a ? which is known as “wrapping our values in an optional”. This is because we are indeed literally wrapping our value in Swifts optional type, thus giving it value. To get a better picture as to whats going on, we can look at how Swifts Optional enum.

enum Optional<T> {
    case None // has no value
    case Some(T) // has some value
} 
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
  • Thank you for your reply. Your answer is really helpful. You talked about some other languages. Now I like swift more and more.) – JW.ZG Dec 19 '15 at 05:58
  • No problem, Swift is a neat language for sure and now with it being open source we may actually see it as a server side language. – Dan Beaulieu Dec 19 '15 at 06:00
  • But why it is server side? I just start to learn Swift. I saw some documents said there are two main parts of swift, UIKIT and cocoa. we can use UIKIT to do many UI design. Please tell me if I misunderstood that. – JW.ZG Dec 19 '15 at 19:23
  • You did. Server side code is like PHP or ASP.NET. I was basically saying that I would like to use Swift on the server and now that it's open source, we may just see that. @JW.ZG – Dan Beaulieu Dec 19 '15 at 21:09