Swift vs Flutter architectural differences from the developer’s point of view
Introduction
We do have many programming languages nowadays, with which we can create mobile apps. The two most recognizable platforms are iOS and Android. The first is supported by Swift and the latter by Kotlin language.
Each of them has been invented to build mobile apps, and both give developers a massive amount of tools to do that.
These are native approaches. However, there are also hybrid solutions. Flutter from Google is one of them. What does it mean `hybrid` in this case? Well, it means that the app for both platforms, iOS and Android, is developed with one tool and in one programming language.
In this article, I would like to point a couple of differences from the developer’s point of view. I would like to focus on the architectural differences between Flutter and Swift.
IDE — Integrated Development Environment
iOS
Apple provides its own tool for developers — Xcode. It is clean and intuitive. It provides everything that is needed for professional development:
- a graphical tool to create views,
- hierarchy view — live app preview with the possibility to zoom, rotate and check each object in 3D (image 2),
- instruments — a built-in tool to investigate resources management, helpful to find, e.g. memory leaks,
- easy Git management,
- app building and deployment process.
What is more, the folders and files structure is clean and easy to understand even for those who know nothing about Xcode yet. All necessary tools are accessed with a button and/or have a clean graphical interface.
What is more, Xcode provides an incredibly expanded auto-completion functionality. What is it about? Well, when you start typing, Xcode suggests possible matching code you may want to enter. It is presented in the form of a list popup. You may choose one of the options or keep typing. If you pick one of the Xcode’s suggestions, a ready code will be pasted. How wonderful! It works with all built-in functions, classes, attributes etc. but also with everything that you have already implemented. That is not everything. Swift uses many `closures` (they are called `callbacks` in Flutter), and they are also covered with the auto-completion tool. Te only thing you need to do is to press Enter and the code will automatically be filled. It makes writing code so much faster than in Flutter!
Let us look at two examples.
Example 1 — Xcode’s suggestions list
Example 2 — closure
Input from Xcode’s suggestions list:
After Enter button pressed on <transform: (Int) throws -> T>:
Flutter
On the other hand, Flutter does not have its IDE tool. For some, the option to choose whichever tool one prefers can be either a blessing or a curse. There are two recommendations of IDEs:
- Android Studio
- Visual Studio Code
The first one tends to run pretty slow, and its interface does not belong to those being intuitive. The VS Code instead seems to be the right choice for those who would rather like cleaner interfaces.
Using Flutter, we have to know that almost everything is accessed through a terminal. Unfortunately, VS Code does not provide us with an auto-completion tool, so we need to remember the code syntax or look at the function or class definition preview.
Comparison
Summary
Both tools, Xcode and VS Code have their interface to create an app. Besides, both provide all the necessary tools and functionalities to manage the project. However, Apple’s developer tool has at least one powerful advantage over Flutter IDEs — the live view hierarchy 3D preview. It gives the developer the possibility to investigate the views hierarchy when the app runs. The preview is rotatable, scalable and adjustable. The tool also points memory problems on individual views.
To conclude, it is much the developer’s preference on which IDE is better for him to use. If the access to Android and iOS projects source codes is a key feature, then Flutter with VS Code or Android Studio would be better. On the other hand, if there’s no need to create an app simultaneously for both platforms, Xcode would be a better choice, mostly because of the clean and very easy to understand interface and intuitive access to all developer tools and functionalities.
Dependencies and general code management
Frameworks
Every developer knows that dependencies appear in every project. If possible, developers try to use built-in frameworks to achieve their goals. Unfortunately, sometimes certain functionalities are not present in the chosen programming language and therefore, developers have two ways to handle the problem:
- Write the code on their own
- Use third-party libraries
Firstly, we might create the code on our own and gain stability and full management power over the functionality and its source code.
On the other hand, we may use third party libraries. In this case, we save some time on development, but we become dependent on other code authors.
If we create a project that needs stability and flexibility, it would be better not to use the work of others. We cannot be sure that a third party code would occur as stable and flexible as we need. What is more, there is always some uncertainty whether the authors would update their frameworks with the next language version release. Here comes an essential difference between Swift and Flutter. Apple provides a huge amount of built-in frameworks that covers an incredible amount of functionality cases. Flutter is much more limited. Using Flutter, we are forced to either write the functionalities on our own or use external libraries. This is a considerable disadvantage in comparison to Swift.
Classes exposure
This topic seems to be very trivial, but it is not. Well, if you write the code in Swift and create the classes in separated files, those classes are exposed to the whole project until they are marked private.
It is different in Flutter. If you like to access a class located in another file you have to write a header in the current file to access it. Now, let us say you keep your code reusable and have many classes that you would like to access. You would need to import all of them to do so. It is nice that the VS Code reminds you about doing that and imports needed files if you allow it. Unfortunately, you still need to manage the import headers in case of using the classes or delete the headers when you do not need them anymore. That would not be a problem when using Swift.
Class extensions
All developers should keep the code clean and easy to understand. We can write classes in separated files and extend their functionalities. Both Swift and Flutter provide this kind of functionality but there is a key difference between them.
Extensions add new possibilities to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code. We can extend the class very easily with an `extension` prefix in Swift:
extension MyClass
or with `extends` expression in Flutter:
ClassA extends ClassB
One widespread use case for extensions is adding protocol conformance to existing types. For example, we can use an extension to add TableView’s delegate capabilities to an existing custom model class.
What is more, the extension may also be used to define a clear structure.
Example — Services structure in Swift
This allows us to structure the code even more. Now we can access the `initRegister` function just with `Services.ProfileServices.initRegister`. This is not possible in Flutter yet.
Summary
Comparing Swift and Flutter in terms of dependencies and general code management, we can see more pros on Swift’s side. Unfortunately, Flutter is highly dependent on external libraries. It may cause problems in the future in case plugins are not supported anymore or contain errors. Flutter may need many external libraries to be fully functional, while Swift can still stand without them.
There are also smaller differences in classes exposure or in capabilities of extensions creation. Both languages provide such functionalities. However, Swift gives us a bit more features.
Networking — data parsing
One of the most common implementations developers need to come up with is data parsing while communicating with server services. Here we find another significant difference between Swift and Flutter.
Flutter
We should remember that Flutter is younger than Swift, and it still needs much work to be done. Of course, data parsing can be handled in Flutter but it seems to be, well, old-fashioned in comparison to Swift.
In Flutter, we have to work with a key-value structure to parse the data coming in JSON format.
Example — Transaction model
Swift
Swift meets the developers' needs and makes data parsing much simpler with `Codable` type that conforms to both `Encodable` and `Decodable` protocols. It is a type that can convert itself into and out of an external representation. Let us take the same model example as in Flutter section.
Example — Transaction model
Summary
Unfortunately, Flutter is still under construction and much work has to be done. Data parsing needs to be handled in an old-school way based on key-value operations. It takes much space, is time-consuming and vulnerable to mistakes. Also, Flutter enums support is minimal, and it is so inconvenient that many Flutter developers even recommend not to use it.
Thankfully, Swift is much more modern. Enums are very common is use and well handled. To continue, thanks to `Codable` preparing the data models for the decoding and encoding process is swift and easy. What is more, this approach is resistant to server changes. It means that if the server sends an attribute that does not exist in the Swift model or does not send an attribute that is defined — the app would not crash.
Creation of User Interface
Programming is not only about functionalities implementation but also the user interface. Every language for mobile development gives us the tools to create it.
Flutter provides a coding style for that purpose and a live preview for our work. The code is spotless, comfortable to understand and structured. It looks a bit similar to CSS. In Flutter, we can find a vast amount of possibilities because of numerous ready-to-use objects that are configurable and easy to combine.
In Swift, we do have a couple of possible approaches to create the user interface. Firstly, developers may write the whole interface with `UIKit` framework code. Unfortunately, there is no option to see a preview — we need to run the app to see the results.
Another way is to use `xibs` and `storyboards`. It is much more user friendly because it is a graphical interface where we place objects to adjust them. The result is continuously visible, but it should be treated as a scheme. A live preview is also possible when the app runs.
The last possibility is to use a brand new `SwiftUI` framework. It is very similar to the Flutter’s approach but still under construction, so very limited so far.
When Flutter and when Swift?
Both Flutter and Swift are powerful languages that make mobile development easy and full of new options. Each of them has some unique characteristics that make them stand out of the crowd. To make a clean breakdown, I would like to present the key differences in a table.
I may recommend Flutter for quick and straightforward projects to develop a beautiful app for both Android and iOS at once. In my opinion, it is not ready yet to handle more advanced, challenging projects because of many limitations.
For all other cases, I would choose Swift. It is powerful and advanced in user interface creation and architectural meaning.