Is learning Swift worth it? – My experience with Swift as a prior React Native developer

Is learning Swift worth it? – My experience with Swift as a prior React Native developer

React Native, Swift or Flutter. Which one comes out on top?


Preface

I have been a React developer for the majority of my years.

I've witnessed React evolve from Class Components to Stateless Functional Components, and then to what we have now: Functional Components with Hooks.

While this article might seem biased, I assure you that I strive to be reasonable, logical, and objective in any judgments and conclusions I make. I pour my heart and mind into this endeavor.

What you are about to read is just my two cents and should not be taken to heart.

You can view the repository for Thoughtful here.

With that, the story begins prior to WWDC 2024.


About a week before WWDC24...

It's about a week away from WWDC 2024. News outlets and YouTube channels are scrambling to be the first to upload reported leaks about the new iOS 18. Features such as customization of the control center, which surfaced last year, are making a comeback.

Amidst all this chaos, Apple Developer's YouTube channel started bulk uploading WWDC 2023 sessions. At that time, it was my first time ever watching a WWDC session, so I clicked on the most interesting video and gave it a watch.

I was pleasantly surprised. Even though I don't really understand Swift, I could infer the language syntax. What caught my eye was how seamlessly Apple's libraries and frameworks work together. It's like butter. Everything just works! Need a map? Import MapKit. Need tips to appear in your app? TipKit!

While watching the session video, I remembered that I had bookmarked an official tutorial guide by Apple, "Develop in Swift," which I had been longing to follow.

Deep down, I had bookmarked the tutorial just in case I got bored and needed something to do. Additionally, I had been eager to learn Swift and iOS app development after following a Udemy course but gave up halfway through.

Since WWDC 2024 is about a week away, I decided to take my chances and follow the tutorial. Maybe I could keep track of WWDC 2024's new Platform State of the Union changes?

Is "Develop in Swift" any good?

I don't know who is the target audience for the guide but all I knew was that it wasn't targeted towards individuals who have never programmed in their life.

Even I had trouble wrapping my head around some of Swift's concepts, such as closures and trailing closures. It's really a syntax issue that I need to get used to. Other than that, it is strongly-typed and comes with neat features like computed properties.

The tutorial guide covers the basics of Swift, building an app in SwiftUI, integrating data persistence using SwiftData, and more. One thing worth highlighting is that the analogies used in the tutorial guide are easy to grasp and really good. They make difficult concepts digestible!

I, shamelessly, haven't completed the tutorial guide in full and stopped after the part on SwiftData as I tried to integrate these concepts into a project I started long ago, but this time, on iOS.

Learning Swift through building an old project I started

For those that didn't know, in February/March this year, I decided to impart some of the skills and knowledge I learned throughout my Mobile App Dev internship by building an app called "Thoughtful." Thoughtful is a daily gratitude journal app that prompts the user to journal about their life.

After a few days of designing in Figma and perfecting every user interaction, I built the app in Flutter. I stored the data in SQLite, on-device, and used Flutter BloC for state management. While it was a huge learning curve to understand BloC (I still detest BloC to this day), my states are kept in sync, and that is what is important.

Now that you know about Thoughtful in Flutter, I tried to build Thoughtful for iOS. It was a complete redesign (though sticking to some elements) and adhered to Apple's Design Guidelines.

Yes, I know that Flutter is cross-platform. I built Thoughtful for iOS as a way to cement my knowledge from the tutorial guide.

What I loved about Swift, SwiftUI and SwiftData (as compared to React)

Declarative approach to building User Interfaces

As I was developing using Swift and SwiftUI, I couldn't help but notice how similar it is to Flutter. Both SwiftUI and Flutter have a declarative approach to laying out a UI.

Declarative, in this context, means laying out a UI involves me "telling" SwiftUI what I want instead of "making" the element in SwiftUI into what I want.

In this case, declarative would be Button().background{RoundedRectangle(cornerRadius: 24)}.

Imperative would be React-like where we have to declare a Component ("Button") and use styling to style the Button – i.e. I make the Button in React into what I want

<Button style={styles.buttonStyles} />

This declarative approach makes it easy to prototype UI but at the same time can make the codebase messy as styles might be everywhere (instead of having one source of truth, like a stylesheet file).

However, it is nice to know that colors (such as background or foreground) are propagated throughout the children when applied at a top-level view.

Packages tend to work, or at least the ones I used do

A big gripe I will forever have with React Native (and Expo) is that packages would break after upgrading either React Native's or Expo's version.

With every Expo SDK upgrade, I would spend more time modifying components, installing compatible packages, or replacing packages.

There was a time when I had to debug for 1.5 hours because the react-svg-loader package broke, and the error message didn't have anything to do with the package.

On the other hand, almost everything is built into SwiftUI. Tooltip? Tab Navigation? Stack Navigation? It's built-in. No need to install packages that might break one's workflow.

SwiftData is a Lifesaver

SwiftData is a way for developers to persist data in their apps. The underlying technology is SQLite (the same as in Thoughtful Flutter), but this "wrapper" allows us to read, insert, update, and delete as if we're interacting with objects and classes rather than doing what we're used to as developers (ID lookups and whatnot). It's really easy!


// Read "Item" from storage and sort it by the date_created
@Query(sort: \Item.date_created) var items: [Item] = []

// Insert
modelContext.insert(Item(...))

// Update – Notice how I didn't even have to touch the modelContext as the Item struct has all the bindings with SwiftData. Editing the object directly would update the Item in the database
Item.date_created = Date.now

// Delete
modelContext.delete(instanceOfItem)

iOS Previews and View Previews

The iOS Previews and View Previews allow us to quickly preview views in a side panel (called "Canvas"). I found it intuitive and fast. It hot-reloads too!

Quality (and quantity) of documentation and support

The quality and quantity of support available for Swift is mind-blowing. Apple's official documentation includes comprehensive guides that have been invaluable.

YouTube channels like Sean Allen's have provided me with more than enough knowledge to build this project.

Where React shines

Cross Platform

That's the biggest driving factor behind basing an entire project on React Native. While React Native may not be as performant (due to having a layer of JavaScript engine running in the application), for small teams, it makes sense to sacrifice some performance for a better developer experience (DX). Moreover, we can build for iOS and Android using a single codebase.

Maturity

Yep, I said it. React Native is more mature than SwiftUI and Swift. This maturity also means better packages (if needed) and better documentation for those packages (for instance, Spotify's iOS API documentation is in Objective-C).

It's easy for developers who's familiar with React

Yeah, it's true. React developers would likely find the switch to React Native much smoother compared to switching to Swift. Even switching to Flutter and Dart would be smoother than switching to Swift.

My biggest gripe with iOS App Development – The App ID Limit

The signing limit really frustrates me, especially as a developer who's not enrolled in the Apple Developer Program (it costs $99 USD/year, but I'm just a student with no budget).

This issue arises when I try to run the build on my own device to test out the widgets I made. Unfortunately, I can't, and I have to wait 7 days.

You might ask, "How did you use up all 10 App IDs already?" My answer would be that I followed the "Develop in Swift" tutorial, which guides you through building multiple apps.

Conclusion

Apple's primary message and agenda in recent years have been to make their tools more accessible so that anybody can code. However, Swift and SwiftUI introduce many abstractions that novice developers might initially overlook.

I want to clarify that I'm not gatekeeping. I would be proud to see a novice developer build an app from scratch, but these abstractions could pose challenges in later stages of development for beginners. For instance, they might wonder, "What if I want to build a Kotlin Composable version of this app?"

This is my opinion, but I'm confident that determined novice developers would find ways to overcome these challenges.

Swift offers numerous language features that simplify development, and Xcode's deep integration with Simulator and macOS (enabling apps to run directly on your Mac) makes it compelling for developers to create apps across multiple Apple platforms (iOS, iPadOS, macOS, watchOS, tvOS, and VisionOS).

SwiftUI has emerged as a leader with its declarative approach to UI development and simplified state management. Gone are the days of setState() and React.createContext(). States and contexts are now more intuitive, not to mention the straightforward approach to data persistence with SwiftData.

In conclusion, Swift is easy to learn and encourages developers to create better mobile apps through tightly integrated APIs like TipKit, SwiftData, WalletKit, WidgetKit, and more. These APIs enable developers to think less and accomplish more – which should be at the heart of every app.

For React Native developers, adopting Swift can enhance app personalization for their Android counterparts. While React offers native APIs that interface with the mentioned APIs, having Swift in your skillset offers unique advantages.

Flutter developers transitioning to Swift will find the switch relatively straightforward. Concepts like states transfer smoothly from Flutter to Swift (and vice versa with React to Flutter), and mastering Swift within a day or two is achievable.

Did you find this article valuable?

Support Nabil Ridhwan by becoming a sponsor. Any amount is appreciated!