React Native vs. Swift for iOS development
If you’re looking to build an iOS app, you’ve got a fair number of languages to work with. React Native and Swift are two such tools to do so.
React Native is a JavaScript library from Facebook used to create cross-platform apps compatible on both iOS and Android. It allows you to use a familiar syntax to leverage native capabilities. Swift is a programming language developed by Apple that has gradually overtaken Objective-C. Swift is used to build iOS applications exclusively.
Before choosing between the two for iOS development, the first question to ask yourself is whether you intend to build a cross-platform app or an iOS-native app. There are other factors to consider, of course, before making this choice.
In this article, we’ll assess these two technologies against a number of factors, from performance to ease of use. Here’s what we’ll discuss:
- How React native builds an iOS app
- How Swift builds an iOS app
- React Native pros and cons for iOS development
- Swift pros and cons for iOS development
- Which is better for performance?
- Which has the faster development lifecycle?
- When to use Swift and when to use React Native
How React Native builds an iOS app
React Native works similarly to React except that React Native doesn’t use the virtual DOM to manipulate the DOM. Under the hood, React Native interprets the JavaScript code and uses the APIs to communicate with the native components. All data are being serialized in a bridge, which disallows user interaction.
React Native is generally known to have two separate parts: the JavaScript code and the native code (iOS and Android). What actually connects these two is a bridge that allows both sides to communicate despite being disparate languages. All data sent to the native code from the JavaScript transmitted as serialized JSON objects. The data could be your UI elements, functions, etc.
In the end, our native iOS modules like FileReader
, DeviceInfo
, and others communicate with the iOS SDK, implementing their corresponding native functionality.
How Swift builds an iOS app
Swift was developed to replace Objective-C in building Apple software. It is included in Xcode, Apple’s IDE for writing and developing applications. Swift uses the LLVM compiler, which is still include in Xcode.
React Native pros and cons for iOS development
No matter how great and reliable a language is, it inevitably has has some downsides. Just because a company chooses Swift or anything else over React Native doesn’t mean it’s a bad tool for mobile development; rather, it doesn’t fit their business or product use case. Generally speaking, React Native offers a fair number of pros.
Pros
- Low learning curve for non-mobile developers: Web developers can jump right into mobile development using React Native because, after all, it’s just JavaScript code. Even if you still have to learn some native concepts, the time for learning seems shorter Hot reloading: UI changes from your code does not require you to rebuild your application. This also aids a faster development and bug fixes.
- Component-based: You can write code and reuse it. Components such as buttons and cards can be used everywhere within you codebase or even in other projects — no need to rewrite the same stuff. Cost-effective: This might not be reasonable across every market, but in general, the cost of building an iOS application is time-saving, has low cost or convenient and pennywise.
- Big community: You should how important this is. The bigger the community, the quicker the help. Imagine most (if not all) of your questions already answered on Stack Overflow — that’s a huge help.
Cons
- Unforeseen changes in update versions.
- Scalability issues when it comes to large projects. Complex software that relies deeply on device hardware tends to experience performance and scalability issues if it isn’t built with native code. Keep in mind that the bridge React Native uses to connect JS code and native code still uses additional layers, which may impact performance.
Swift pros and cons for iOS development
Swift was built to be used for all Apple operating systems, i.e., iOS, iPadOS, macOS, tvOS, and a few others. Swift’s primary benefit is in its name — it’s fast. But, as always, there are cons to weigh against the pros.
Pros
- Type-safe: Swift provides a safe and strong typing system with a good error handling style. This prevents or reduces bugs to the greatest extent possible.
- Scalable and maintainable: Swift allows you to focus on the core infrastructure of your application. A simpler syntax makes code easier to read and maintain.
- Performance: Swift was created to outshine Objective-C as a means of developing mobile apps. Apps built with Swift have been shown to be 2.6x faster than a similar app built with Objective-C.
- Backed by Apple: Swift is built and maintained by Apple, so you can safely assume your app will be future-proof. It is also open source.
Cons
- Lack of support for previous versions.
- Relatively costly/competitive job market to find dedicated Swift developers.
- You have to use Xcode and work on a Mac and other Apple devices exclusively.
Which is better for performance?
True native development with Swift wins for performance — not because it is Swift, per se, but because it allows for direct access to native device hardware and APIs.
That doesn’t mean React Native isn’t performant; it is by default. With that said, there are certain measures you need to take to ensure your app is fast and reliable when using React Native for iOS development:
Use an alternate image caching component
Consider using the external react-native-fast-image for rendering images. React Native does provide an inbuilt Image
component, but it can sometimes lead to poor performance, especially when it comes to flickering or handling a lot of images on the screen.
Use Hermes
Hermes has been available for APK for Android for a while now, and it has helped ti reduce app download size, memory usage, and more. As of 12 March 2021, Hermes now supports iOS as well. Likewise, it also reduces download size, decreases the time for the app to become usable, and reduces memory usage. Hermes is still in the early stages for iOS support.
To use Hermes in your application, set hermes_enabled
to true
in your Podfile and run pod install
.
use_react_native!(
:path => config[:reactNativePath],
# to enable hermes on iOS, change `false` to `true` and then install pods
:hermes_enabled => true
)
Embrace memoization
This is a specific type of caching that returns the same value of a function that has already been cached. This alone speeds up programs and can improve performance.
I have had to explain more on performance optimization for React Native because Swift is clearly the winner here — it’s native, and there’s not much more to say than that. But that doesn’t mean React Native is not powerful enough for certain use cases.
Which has the faster development lifecycle?
If your ultimate goal is to release your app for both iOS and Android, React Native carries this criterion handily thanks to its ability to build one application for both platforms. Also, React Native components are mostly from the web (React) so they can be copied/pasted, which speeds up development.
Additionally, React Native provides hot reloading and live reloading features, which means you won’t have to rebuild your application multiple times. This alone fosters a fast development lifecycle.
When to use Swift and when to use React Native
React Native and iOS are two powerful tools that do their respective jobs great. The choice to use one or the other now lies in your hands and ultimately depends on the type of project you want to build.
This is what I mean. Swift will work better for:
- Resource-intensive apps with high memory management needs and sophisticated UIs.
- Apps that will definitely extend or connect to other Apple products in the future — in other words, apps that are predominantly meant for iOS and macOS users.
- Apps that will need upgrades or maintenance over the long term.
React Native will work better for:
- Apps that need to be developed in less or limited time with a few developers.
- Apps that require cross-platform capabilities.
Conclusion
In sum, Swift will produce a more performant iOS app by default, but it is still good to know that RN offers strong performance and a good DX out of the box, particularly now that Hermes has helped to reduce download size and memory usage. At the end of the day, choosing the right tool for your app requirements is definitely the best thing you can do.