Swift vs. ArkTS: A Practical Guide to HarmonyOS Development

I've been learning ArkTS UI recently and noticed it looks a lot like SwiftUI, so I put the two side by side.
Comparing the Hello World code
Judging by Hello World alone, the two are very similar.
Here's the Hello World for HarmonyOS ArkTS UI:
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
}
.height('100%')
.width('100%')
}
}
And here's the SwiftUI Hello World:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
#Preview {
ContentView()
}
Same struct opening, same declaration style, same Text, same methods for setting appearance attributes.
The difference is that ArkTS follows TypeScript conventions: it starts with decorators, uses the build method as the entry point for initialization, and the elements live inside build. In SwiftUI, the whole ContentView is one big element, and then you start nesting elements inside it.
Declarative UI description
Obviously, both use a declarative UI description.
My own summary of the declarative UI formula looks like this:
Element(props) {
SubElement...
}.attribute(value)
元素(元素属性配置) {
子元素
}.元素外观属性(元素外观)
Because ArkTS is essentially TypeScript / JavaScript, its syntax has to follow TS/JS conventions: any property or variable you reference has to be explicitly indicated.
Text(this.message) // message 是当前页面引入的,要有 this
.id('HelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold) // Bold 是公共常量 FontWeight 的其中一值
For frontend developers, it's simple and easy to read. Here Text takes a message, then tags the id as HelloWorld, sets the font size to 50, and sets the font weight to bold.
Swift, meanwhile, probably suits people who came over from Apple's older Objective-C way of writing.
Image(systemName: "globe") // 公共库名:公共库的值
.imageScale(.large) // .large 是公共常量的一个值
.foregroundStyle(.tint)// .tint 是公共常量的一个值
Here Image pulls in the globe icon from the shared icon library, then sets the image scale to large and the foreground style to the system theme color. Image(systemName: "globe") clearly doesn't match the convention of defining a class with new, and .imageScale(.large) and .foregroundStyle(.tint) don't match how arguments are normally used either — reading them the old way is a bit baffling.
Converted to TypeScript, it should look like this:
new Image(SystemIcon.globe)
.imageScale(CommonSize.large)
.foregroundStyle(CommonColor.tint)
Clearly ArkTS fits frontend developers' reading and writing habits better. But mastering SwiftUI isn't actually hard — you just need to remember these small differences, and after writing it twice you'll get the hang of it.
The coupling debate around declarative UI
Plenty of people may be put off by the coupling in declarative UI (M and V coupled together). But on the frontend, apart from the old MVC framework Angular.js, the other frameworks — even MVVM — rarely manage to decouple, especially the coupling between a single feature and its business data interactions.
So frontend coupling is still something you have to handle yourself.
Button({
action: handleGoButton
}).{
Text("Go")
}
// controller 事件,自行决定 handleGoButton 是否需要放在外部文件中
private func handleGoButton() {
...
}
// 数据耦合在 UI 中,无解
ZStack {
Color(selectedImageIndex == index ?
Color.hex("808080") : Color.hex("585857")) // 选中的背景颜色区别一般的
...
}
In the end, decoupling on the frontend still comes down to componentization and higher-order functions:
-
Componentization: by breaking the UI into independent components, each with its own functionality and state, you can reduce coupling further. Componentization lets developers build and test components independently, without worrying about how the rest is implemented.
-
Higher-order functions: in some declarative UI frameworks, you can use higher-order functions to reuse shared logic while allowing unique logic to be swapped in. This cuts down code duplication and makes components more reusable.
Component differences
SwiftUI and HarmonyOS's ArkTS UI framework both offer a wide range of components, and from a frontend usage standpoint they actually share a lot in common too.
Basic components
The basic components are essentially the same:
- Text for displaying text;
- Image for displaying images;
- Button for letting users tap to perform an action;
Layout components
Here are the layout components that SwiftUI and ArkTS share or closely mirror:
| SwiftUI | ArkTS | Description |
|---|---|---|
| HStack | Row | Horizontal stack, for arranging subviews horizontally |
| VStack | Column | Vertical stack, for arranging subviews vertically |
| ZStack | Stack | Stack view, for layering multiple views |
| Spacer | Blank | Blank component, for taking up the remaining space |
| ScrollView | Scroll | Scroll view, letting users scroll content |
| TabsView | Tab | Tab view, for building tabbed navigation |
| NavigationView | Navigation | Navigation view, for building a navigation structure |
As you can see, the two are largely interchangeable when it comes to layout, though the details will certainly differ in plenty of places. Still, translating one into the other shouldn't be hard — you could try having AI do it.
Where they differ is in Flex and Grid layout:
-
SwiftUI only has lazily loaded components: LazyVGrid and LazyHGrid, lazy-loading grid views for displaying large amounts of data.
-
ArkTS UI has: Flex, a flexible container component, for flexible layouts; and Grid, a grid layout component, for creating grid layouts.
SwiftUI's layout system is very flexible: you can meet complex layout needs by adjusting properties like alignment, spacing, and padding. It isn't exactly equivalent to Flexbox and Grid in CSS, but by combining different layout views you can produce rich and varied layout effects.
From my own experience, in real iOS development you can implement flexible layouts and grid layouts perfectly well just by combining the basic layout components.
Form components
ArkTS UI provides a set of components that are closer to their HTML namesakes:
- TextInput: for text input.
- CheckBox and Switch: for boolean selection.
- Radio: for radio button groups, similar to radio buttons in HTML.
- Picker: for pickers, usable for date selection, time selection, or simple option selection, similar to
<select>in HTML.
What stands out about ArkTS UI form components is how tightly they're integrated with data binding: you can manage form state and data flow through decorators such as @State, @Link, and @Prop.
SwiftUI has similar form components too, but most of them aren't the same:
- TextField: for text input, and it can include placeholder text.
- SecureField: for password input, hiding what's typed.
- Picker: for pickers, which can select a single value or multiple values.
- Toggle: for boolean selection, like a switch.
- DatePicker: for date and time selection.
- Slider and Stepper: for numeric selection, where
Slidergives continuous value selection andSteppergives stepped value selection. - Form: a container view for organizing input form data, making forms easier to create and manage.
They don't map one to one, but for things like date pickers, most users have already gotten used to the differences between Android and iOS.
Conclusion
This side-by-side study leads to a few conclusions:
- Declarative UI is easier for frontend developers to configure and read, especially ArkTS;
- Decoupling requires you to handle it yourself using componentization, higher-order functions, and the like;
- ArkTS UI and SwiftUI's basic and layout components are highly similar and largely map one to one, so you can learn them side by side;
- Given how similar they are, you can try building one app and then translating it to the other with AI.
I don't think the fourth point is very hard. I've successfully translated an Android app with very different code into an iOS app; it just took a lot of time debugging page by page.
As for which app to build first, that comes down to your personal habits. If you're a longtime iOS developer, you can build for iOS first and then develop for HarmonyOS; you could even build with React Native to produce iOS code, then translate the generated iOS code into a HarmonyOS app.
If you don't carry that previous baggage, you can absolutely learn ArkTS first since it's faster to pick up, then learn Swift by translating iOS apps.