Swift vs. Objective-C in 2019
Objective-C vs Swift in 2024 | Swiftify
Objective-C was created by Brad Cox and Tom Love in 1984 as an extension of C. It added Smalltalk-style messaging and Object Orientation to the C language.Comparative advantages of Objective-C:Interoperability with C++ and Objective C++Dynamic features like method swizzlingDisadvantages of Objective-C: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 the 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.Explicit pointers.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.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:Swift is safer due to static typing and the use of optionals and optional chaining.Support for namespaces, a clear mutability syntax, functional patterns, and concise syntax.Interactive development using Playgrounds.Swift is easier to learn for new programmers, and the official language guide by Apple is a great resource.Swift is performant and finds its place in server-side applications. The advantages of using Swift on the server side are explained in this talk on Realm Academy by Chris Bailey, where he points out the benefit of Swift over other frameworks on the server and the cloud. According to him, Swift is highly performant and has a low memory footprint, making it an ideal choice for server-side development. We use server-side Swift in production for our Storyboard → SwiftUI Converter.Swift is now mature with its ABI locked down and Module Format Stability.The Swift standard library code contains around 42.9% of its code in Swift. The split up of the different languages used in the standard library is shown in the picture below. This Swift code is probably the best Swift code that developers can refer to improve their Swift coding. It is covered very well in this talk.8. SwiftUI is a Swift-only declarative framework for making UI for multiple platforms with built-in support for dark mode and other accessibility features. SwiftUI can be used within UIKit, and UIKit can be used within SwiftUI.9. Combine, which is s Swift-only framework for writing functional reactive code. It provides a declarative Swift API for processing values over time.10. The preview of the SwiftUI is available right with Xcode without running the project on the simulator (giving instant visual feedback). Also, the preview device can be switched on the fly by adding a modifier for a preview device without the hassle of building and running the project on a different simulator.11. Swift is cross-platform and can be used for development available across macOS, Ubuntu, CentOS, Amazon Linux, and Windows.12. Swift has great packages like Swift Numerics for Numerical Computing, Swift Algorithms, which include a host of powerful, generic algorithms frequently found in other popular programming languages and many other excellent packages developed by Apple.13. Swift 5.9 introduced Macros, which provide a new way to extend the compiler with custom compile-time checks and code generation.Comparative Disadvantages of Swift:Higher compile time.No direct way of using C++ libraries.ConclusionSwift is stable and has been the constant focus of Apple with continuous language improvements, exclusive frameworks like SwiftUI and Combine — and other great packages like Swift Algorithms, Swift Collections, etc. The latest release of Swift improves performance and efficiency and introduces new frameworks like RegexBuilder. Learn more about these changes here.We recently studied Swift usage amongst the top 100 non-game apps by revenue on the AppStore. Our study found that 90% of the apps used Swift in their main binary. According to a similar by Andrew Madsen in 2019, 57% of the apps used Swift in the main binary.Apple offers great interoperability between Objective-C and Swift and is not abandoning support for Objective-C anytime soon, but at this point, migrating from Objective-C to Swift is worth the investment.If you are using C++ and Objective-C++ codebase, you will 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. Swift 5.7 greatly improves interoperability with C APIs and the community is actively working on adding support for C++.Confused about which parts of your project you should migrate to Swift? Please refer to this article, which is covered in detail.References:https://cocoacasts.com/namespaces-in-swifthttps://academy.realm.io/posts/tryswift-chris-robert-end-to-end-application-development-swift-backend/https://youtu.be/jWaO3rsNZU0https://youtu.be/wB-bi8_rSLshttps://swift.org/https://www.avanderlee.com/swift/combine/You can contact us on Twitter, LinkedIn, Facebook, and GitHub.