Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes Swift's "Optional" safer than Objective-C's "nil"?

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 Optionals safer than nil, and how will this affect my code?

like image 347
Pratap Vhatkar Avatar asked Feb 22 '16 05:02

Pratap Vhatkar


People also ask

Why is Swift safer than Objective-C?

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.

What is optional in Swift and nil in Swift and Objective-C?

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.

What is the difference between Objective-C and Swift?

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.

Why do we need optional in Swift?

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.

Is Objective C more secure than Swift?

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.

What is Swift language?

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.

Why is Objective C a well tested language?

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.

What is the advantage of Objective C over C?

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.


1 Answers

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:

  1. Enforce good programming practices.
  2. Allow for better code checking at compilation time

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.

like image 54
Sulthan Avatar answered Oct 04 '22 17:10

Sulthan