Mobile

Swift vs Objective-C in 2019

Comparative advantage of Objective-C:

  1. Interoperability with C++ and Objective C++
  2. Dynamic features like method swizzling
  3. Better support for writing Binary Frameworks.

Disadvantages of Objective-C:

  1. Since Objective-C is built on top of C, it lacks namespacing. All classes in an Objective-C application should be globally unique. So to avoid collision there is a convention of prefixing the names of classes. This is the reason we have the ‘NS’ prefix for the class in the Foundation Framework and the ‘UI’ prefix for the classes in UIKit.
  2. Explicit pointers.
  3. The ability to send a message on a nil object without crashing and the lack of strict typing lead to bugs that are hard to trace and fix.
  4. The language is syntactically verbose and complex, but this is expected given that it is a fairly old language.

Swift is a young language released in 2014. It is designed to be safe and performant with modern syntax and features. Swift was Open-Sourced in December 2015.

Comparative Advantages of Swift:

  1. Swift is safer due to static typing and the use of optionals and optional chaining.
  2. Support for namespaces, a clear mutability syntax, functional patterns and concise syntax.
  3. Interactive development using Playgrounds.
  4. Swift is easier to learn for new programmers, The official language guideby Apple is a great resource.
  5. Swift is performant and is finding its place in the server side applications. The advantages of using Swift on the server side are explained in this talkon Realm Academy by Chris Bailey, where he points out the advantage that Swift has over other frameworks on the server and the cloud. According to him Swift is highly performant and has a low memory footprint which makes it an ideal choice for Server side development.
  6. Swift is now stable with its ABI locked down.
  7. The Swift standard library code contains around 42.5% of its code in Swift. The split up of the different languages used in the standard library is as shown in this picture below. This Swift code is probably the best Swift code which developers can refer to improve their Swift coding. It is covered very well in this talk.

Comparative Disadvantages of Swift:

  1. Higher compile time.
  2. No direct way of using C++ libraries.
  3. Module format stability is still not achieved and is required for developers who want to share their code as a binary framework.

Conclusion

Swift is now officially ABI stable and can be considered to be a mature language. The future updates in Swift would not break the current code written from now on in Swift 5.

Apple offers great interoperability between Objective-C and Swift, and is not dropping support for Objective-C anytime soon. It is better for teams to start migrating parts of their Objective-C code to Swift as it is ABI Stable now.
If you are developing a binary framework I would suggest waiting for Swift to achieve Module Format Stability. Also, if you are dealing with C++ and Objective-C++ codebase or framework, then you would need a mix of Objective-C and Swift. The Objective-C part can interface directly with the C++ or Objective-C++ parts of your code and the Swift part can then use Objective-C classes to interact with the C++ or Objective-C++ code.

Confused about whether you should migrate to Swift and what not please refer to this article where it is covered in detail.

References:

  1. https://cocoacasts.com/namespaces-in-swift
  2. https://academy.realm.io/posts/tryswift-chris-robert-end-to-end-application-development-swift-backend/
  3. https://youtu.be/jWaO3rsNZU0
  4. https://youtu.be/wB-bi8_rSLs

You can contact us on TwitterLinkedinFacebook and Github.

Leave a Reply

Your email address will not be published. Required fields are marked *