Introduction: When you start learning how to build apps for iPhones, you will hear two names: SwiftUI and UIKit. Both are systems used to make screens for your app. But which one should you learn first? Let's understand them in very simple words.
The Restaurant Analogy (Declarative vs Imperative)
Before looking at code, let's understand the two ways of thinking: Declarative (SwiftUI) and Imperative (UIKit).
Imagine you walk into a restaurant and want tea:
- Imperative Way (UIKit): You go to the kitchen. You tell the chef: 'Take a pot, pour 1 cup of water, light the gas, wait 5 minutes, add tea leaves, add sugar, filter it, and bring it to my table.' You give step-by-step instructions.
- Declarative Way (SwiftUI): You sit at the table and tell the waiter: 'I want a cup of hot tea.' You just describe what you want. You do not worry about how the kitchen makes it.
Side-by-Side Code Comparison
Let's see how much code you need to write to show a simple list of items (like fruit names).
How you write it in UIKit:
import UIKit
class ListViewController: UIViewController, UITableViewDataSource {
let fruits = ["Apple", "Mango", "Banana"]
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.frame = view.bounds
view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruits.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = fruits[indexPath.row]
return cell
}
}How you write it in SwiftUI:
import SwiftUI
struct ListView: View {
let fruits = ["Apple", "Mango", "Banana"]
var body: some View {
List(fruits, id: \.self) { fruit in
Text(fruit)
}
}
}Can They Work Together?
Yes! You do not have to choose only one. Apple lets you mix them. For example, if you build a new app in SwiftUI, but you want to use an old map component built in UIKit, you can embed the UIKit map inside SwiftUI using a wrapper called UIViewRepresentable. This means learning one will still help you use the other.
Comparison Summary
| Feature | SwiftUI | UIKit |
|---|---|---|
| Style | Declarative (Say what you want) | Imperative (Say how to build) |
| Code Length | Very short | Long and detailed |
| Layout System | Stacks (HStack, VStack) | Auto Layout Constraints |
| Best For | New apps, fast prototyping | Old apps, complex custom views |
| App Previews | Real-time updates on canvas | Must rebuild and run simulator |
Summary
SwiftUI is the future of iOS development. Starting with SwiftUI is the easiest path for beginners, and adding UIKit basics later will make you a complete professional developer.