Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a non optional variable be nil in Swift? [closed]

Can I create a normal variable in Swift (I mean a non-optional) and assign a nil value to it or later during the app lifecycle, let it be nil?

It confuses me, since it's a little strange compared to traditionally strong programming languages, like Java and C#.

like image 503
George Avatar asked Dec 06 '22 00:12

George


2 Answers

No, that's not possible by design. This excerpt from the documentation explains why:

The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to return nil from a method that would otherwise return an object, with nil meaning “the absence of a 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 typically return a special value (such as NSNotFound) to indicate the absence of a value. This approach assumes that the method’s caller knows there is a special value to test against and remembers to check for it. Swift’s optionals let you indicate the absence of a value for any type at all, without the need for special constants.

You are describing optionals as a bad thing, whereas is one of the features I appreciate more in the language, because it prevents most of the null pointer exception bugs.

Another advantage is that when a function can return a non-value (nil for reference types in objective 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 have. Not mentioning that it's a convention that both the caller and the function/method must follow.

Last, if you are using too many question and exclamation marks in your code, then you should think about whether or not optionals are really appropriate for the problem (thanks @David for the hint), or taking advantage of optional binding more frequently in all cases where optionals are really needed.

Suggested reading: Optionals

Addendum

Hint: I've frequently seen uses of optionals in cases where a variable is declared, but cannot be initialized contextually. Non optional mutable variables are not required to be declared and initialized in the same line - deferred initialization is allowed, provided that the variable is not used before its initialization. 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 feature will let you remove several optionals from your code...

like image 76
Antonio Avatar answered Dec 23 '22 02:12

Antonio


Can I create a normal variable in SWIFT (I mean a non Optional) and assign a nil value to it or later during the app lifecycle, let it be nil.

No.

This is easily testable in 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 an example of how to use it.

like image 26
Aaron Brager Avatar answered Dec 23 '22 01:12

Aaron Brager