Skip to content

JA5. Why Swift?

Apple released a new language, Swift, in 2014 replacing (Mindfire Solutions, 2017) its own language Objective-C. Now, the apps are being developed in swift only, and also the apps which were developed in Objective-C have been migrated into Swift language. Discuss the features of swift that have made it so much popular and are preferred for iOS development over Objective-C.

As We learned about Kotlin and its relationship with Java in the previous weeks; Swift plays a similar role with Objective-C. Swift is a modern language that came to solve problems in older generations that has been there for a while; and has a set of features that makes it special, I will explain each of these features in the following sections (according to (Mindfire Solutions, 2017))

  • Type safety with type inference.
  • Functional first with Object Oriented support.
  • Immutability and mutability rules.
  • Fully compatible with Objective-C.
  • Multiple return values using tuples.
  • Seamlessly supported by Xcode.
  • Run C code within Swift.

Type safety is such an important part of a language, but the language designers need to be careful not make typing verbose and repetitive. Swift has a type interference system that can guess the type of a variable and expression without the need to explicitly declare it, and -as opposed to Objective C- the type of the variable can not be changed after it has been declared. This makes the code more readable and less error prone; along with the generic types that Swift supports, which makes code reuse easier and more efficient.

Swift is designed in an era where developers are shifting away from OOP towards functional programming; thus, Swift comes with functional support and first class functions, where functions can be passed down the chain as needed. Swift also has strong support for OOP, and it is a multi paradigm language that can be used in both ways.

Mutable state was always sort of errors and conflicts as the the value bound in same memory location changes overtime which makes it inconsistent across various parts of the program. Swift has a strong support for immutability using the let keyword, which makes the variable immutable and can not be changed after it has been declared. This makes the code more readable and less error prone.

When Swift was introduced, there are already a lot of apps that were written in Objective-C, switching to SWift is going to be painful and time consuming, and unpleasant journey that nobody is willing to make; thus, Swift is fully compatible with Objective-C, so it can be added to an existing project and can be used alongside Objective-C, where new code is written in Swift and old code is still in Objective-C, which can be migrated to Swift gradually.

Swift has a future to return multiple values using the tuple type, which iw q collection of immutable values. Most languages only support one value to be returned from a function, but Swift supports multiple value returns.

The language is as good as its ecosystem; The Xcode IDE is built and optimized for swift that is so powerful in refactoring, debugging, and developing Swift apps; which makes the developer experience so much better.

Although Swift is great language, there are specific cases and performance critical parts of apps or algorithms that needs to be written in a lower level language like C, Swift can run C code within it, which makes it a great choice for iOS development.

As conclusion, Swift is a modern language that is similar to Kotlin; it is the language for IOS development; I personally loved Kotlin and Swift.

References