Apple seems to claim that the Optional
type in Swift is safer than nil
in Objective-C, but I don't understand why this is so.
What are the salient differences in implementation that make Optional
s safer than nil
, and how will this affect my code?
With Swift, you can compile, and fix the errors while writing the code, which is not possible with Objective-C. As a result, Swift works better and faster compared to Objective-C when it comes to bug testing. All this gives reason to consider Swift as a safe and secure programming language.
In Swift, nil means the absence of a value. Sending a message to nil results in a fatal error. An optional encapsulates this concept. An optional either has a value or it doesn't.
Swift is a general-purpose, high-level programming language which is highly concerned about safety, performance. Objective C is an general purpose language which is considered as superset of C language it was designed in an aim of providing object-oriented capabilities.
An optional value allows us to write clean code with at the same time taking care of possible nil values. If you're new to Swift you might need to get used to the syntax of adding a question mark to properties. Once you get used to them you can actually start benefiting from them with, for example, extensions.
Objective C is not as secure as Swift. An app developed with Objective C is more prone to hack than Swift. Swift is relatively a new language. Apple started to work on Swift in 2010 and it is first released to public in 2014.
Swift is relatively a new language. Apple started to work on Swift in 2010 and it is first released to public in 2014. It has become open source in 2015. Swift follows the features of modern programming languages so, it is easier to learn.
Objective C is well tested language because it has existed from many years. There is a lot of code written in Objective C. It has many well-documented, third-party frameworks.
As Objective C is a superset of C thus, the code of C and C++ runs smoothly on this. Objective C is stable. You don't need to spend money on migrating if you have developed your app on Objective C.
In general, in programming we want to avoid variables that have no value (null
or nil
) because using them often results in undefined behaviour (exceptions, errors, crashes).
For example, it is a common practice for Array
references to be set to empty arrays instead of nil
. On an empty array we can use all the Array
methods, e.g. indexOf
or count
. Using them on nil
would result in a crash.
Optionals allow us to specify that some variables are never empty, therefore we can use them safely and the compiler checks that nil
is never assigned to them. Also, the compiler enforces that every conversion from optionals to non-optionals is explicit (we are always checking for nil
when needed).
So the answer would be that optionals:
Thus preventing programming errors.
Also note that the you should always try to avoid optionals when possible. The greatest power of optionals is the fact that most variables are not optionals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With