Can an optional variable be nil in Swift?

Can I create a regular variable in Swift (I mean not optional) and assign a value to it nil

or later during the lifecycle of the application, let it be nil

?

This confuses me as it is a bit weird compared to traditionally strong programming languages ​​like Java and C #.

+3


source to share


3 answers


You're right, you can't set a non-zero value to zero, although it seems like a burden at first, you get more security and readability while giving away a tiny bit of flexibility. Once you get used to it, you will appreciate it more and more.



+3


source


No, this is not possible by design. This excerpt from the documentation explains why:

The concept of optionals does not exist in C or Objective-C. The closest thing to Objective-C is the ability to return nil from a method that would otherwise return an object, with nil meaning "no valid object". However, this only works for objects - it doesn't work for structures, basic C types, or enumeration values. For these types, Objective-C methods usually return a special value (for example, NSNotFound) to indicate the absence of a value. This approach assumes that the caller knows that there is a special value to test and remember in order to test it. Swifts options allow you to specify no value for any type at all, without the need for special constants.

You describe options as bad things, whereas one of the features I value more in this language is because it prevents most null pointer errors.

Another advantage is that when a function can return a non value (nil value for reference types in object C, -1 for integers, etc.), you don't have to choose a value from the spectrum of possible values ​​that a variable of a certain type can be. ... Not to mention, this is a convention that both the caller and the function / method must follow.

Finally, if you are using too many question and exclamation marks in your code, you should consider whether the options are really appropriate for the problem (thanks @David for the hint), or more often use optional binding whenever additional options are really needed. ...



Recommended reading: Options

Adding

Hint: I've often seen the use of options in cases where a variable is declared but cannot be context-initialized. Optional mutable variables are not required for declaration and initialization on the same line - deferred initialization is allowed, provided that the variable is not used before it is initialized. For example:

var x: Int // Variable declared here

for var counter = 0; counter < 10; ++counter {
    println(counter)
}

var array = [1, 2, 3]

// ... more lines of code NOT using the x variable

x = 5 // Variable initialized here

print(x)

      

Hopefully this function will allow you to remove a few extra parameters from your code ...

+8


source


Can I create a regular variable in SWIFT (I mean optional) and assign it to nil or later during the lifecycle of the application, let it be noil.

Not.

It's easy to check on the playground:

var str = "Hello, playground"

str = nil

      

The second line will get this error:

Type 'String' does not conform to protocol 'NilLiteralConvertible'

You might want to read more about Swift Literal Convertibles and see a use case .

+4


source







All Articles