How are Optional Variables a "powerful solution"?
I was thinking how is this "Powerful solution" according to the apple of optional variables really powerful if this is what we already had in Obj-c?
var mystring: String? = nil
if mystring {
//string is not nil
}
The second script won't compile
var mystring: String = nil
if mystring {
//string is not nil
}
We've been able to do this in Obj-C before without any additional configuration.
NSString * somestring = @"Test";
if(something != [NSNull null]){
//Do something.
}
or
NSString * anotherstring = nil;
if(anotherstring == [NSNull null]){
//display error.
}
so I'm really confused about how this is as powerful as they claim if it already existed in the old language.
source to share
In Objective-C, a pointer to an object can be nil
, yes. But there was no compulsion about meaning nil
.
NSString *shouldNeverBeNil = @"a string!";
shouldNeverBeNil = nil;
NSLog("Hello, %@", shouldNeverBeNil); // "Hello, "
In ObjC, this compiles just fine, although we should never hail to anything. This is mistake.
But if we do the same in Swift, it doesn't even compile and we don't get a runtime error.
var shouldNeverBeNil: String = "a string!"
shouldNeverBeNil = nil; // Compilation error.
NSLog("Hello, %@", shouldNeverBeNil); // never happens
Options allow you to bless variables with the ability to be nil
. Compile errors are always preferable to runtime errors as it is impossible for the end user of your application to execute a compile error.
If you want this value to be nil
, Swift forces you to bless it explicitly, as optional. Now if it is nil
, you have explicitly allowed it, and Swift reminds you to handle both the case handle nil
and the value in your code.
var okToBeNil: String? = "a string!"
okToBeNil = nil;
if okToBeNil != nil {
NSLog("Hello, %@", okToBeNil!); // never happens
} else {
NSLog("What is your name?")
}
source to share
An optional type is itself (actually an enumeration) and it can contain 2 values:
- the actual instance / value of the type that is optional used (corresponds to the enumeration case
Some
) - a
nil
(matches theNone
enum case ) that represent the absence of a value
The difference with other languages such as ObjectiveC is that options do not use a valid type value, which may have a value in some cases and a different value in others.
In object C, the absence of a reference type is represented nil
, which is actually a pointer to location 0x00000000 (in a 32-bit scenario).
Missing a value type instead is usually done by convention. A function that returns an integer can define -1 as no value, but -1 is an integer, and if the function can return negative values, it cannot be used.
In Swift, instead of an optional, there can be either a valid integer value, or None
that is not an integer (nor does it use an instance of a class, structure, or any other type with an optional).
Also, and more importantly, you cannot assign nil
to an optional variable - which results in a compilation error, so it prevents a lot of common errors that are usually found at runtime and are often difficult to track down.
Finally, while in a C object you can use nil
for reference types, you cannot use for a value type (as above for an integer type). In swift, the optional parameter can instead be zero regardless of the type it contains - so it Int?
can be an integer or nil
.
source to share