SwiftUI Essentials
SwiftUI is Apple’s UI framework used to build apps for iPhone, iPad, Mac, and more.
1. What is SwiftUI?
Answer: SwiftUI is Apple’s modern UI framework to build apps for iOS, macOS, watchOS, and more. It uses a declarative approach and requires less code.
1
2
3
4
5
struct ContentView: View {
var body: some View {
Text("Hello, SwiftUI")
}
}
2. What is a View?
Answer: A View is the basic UI element. Everything visible on screen (text, button, image) is a view.
1
2
3
Text("Hello")
Button("Tap me") { }
Image(systemName: "star")
3. What does Declarative UI mean?
Answer: Declarative UI means you tell SwiftUI what you want the interface to show, and SwiftUI handles how to create and update it. Instead of writing step-by-step instructions for every UI change, you simply describe the desired result based on the current data or state.
1
2
3
4
VStack {
Text("Name")
Text("Taylor")
}
4. Declarative vs Imperative?
Answer:
Declarative = describe result
Imperative = write step-by-step instructions
1
2
3
4
5
6
// Declarative
Text("Hello")
// Imperative idea
label.text = "Hello"
view.addSubview(label)
5. What is Composition?
Answer: Combining small views to create complex UI.
1
2
3
4
5
6
7
VStack {
HStack {
Image(systemName: "star")
Text("Rating")
}
Text("5/10")
}
6. What are View Modifiers?
Answer: Modifiers change the look or behavior of a view.
1
2
3
4
Text("Hello")
.font(.title)
.foregroundColor(.blue)
.padding()
7. What is State-driven UI?
Answer: UI automatically updates when data (state) changes.
1
2
3
4
5
6
7
@State private var count = 0
Button("Increase") {
count += 1
}
Text("\(count)")
8. What is @State?
Answer: @State stores local data for a view and updates UI when it changes.
1
2
3
@State private var name = "John"
Text(name)
9. What is Binding?
Answer: Binding connects two views to share and update the same data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct ParentView: View {
@State private var count = 0
var body: some View {
ChildView(count: $count)
}
}
struct ChildView: View {
@Binding var count: Int
var body: some View {
Button("Add") {
count += 1
}
}
}
10. Why SwiftUI uses struct for Views?
Answer: Views are lightweight and describe UI, so value types (struct) are efficient.
11. What is List?
Answer: List shows multiple items from a collection.
1
2
3
List(["Dog", "Cat", "Bird"], id: \.self) { pet in
Text(pet)
}
12. What is ForEach?
Answer: ForEach creates views for each item in a collection.
1
2
3
ForEach(0..<5) { i in
Text("Item \(i)")
}
13. What is Animation in SwiftUI?
Answer: Animations happen automatically when state changes.
1
2
3
4
5
Button("Tap") {
withAnimation {
count += 1
}
}
14. What is Preview?
Answer: Preview shows live UI in Xcode without running the app.
1
2
3
#Preview {
ContentView()
}
15. What is Adaptive UI?
Answer: UI automatically adjusts for dark mode, screen size, and language.
16. Can SwiftUI work with UIKit?
Answer: Yes, SwiftUI can integrate with UIKit/AppKit.
1
2
3
4
5
6
7
8
9
struct MyUIKitView: UIViewRepresentable {
func makeUIView(context: Context) -> UILabel {
UILabel()
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.text = "UIKit inside SwiftUI"
}
}
17. What is incremental adoption?
Answer: You can use SwiftUI step by step without converting the whole app.
19. What is VStack, HStack, ZStack?
Answer:
These are layout containers in SwiftUI used to arrange views.
- VStack arranges views vertically (top to bottom)
- HStack arranges views horizontally (left to right)
- ZStack overlays views on top of each other
They help you structure UI easily without complex layout code.
1
2
3
4
VStack {
Text("Top")
Text("Bottom")
}
20. What is Spacer?
Answer: Spacer is used to create flexible empty space between views. It automatically pushes views apart and adjusts based on available space. It is very useful for alignment.
1
2
3
4
5
HStack {
Text("Left")
Spacer()
Text("Right")
}
21. What is NavigationStack?
Answer: NavigationStack is used to handle navigation between screens in SwiftUI. It replaces older NavigationView and provides a modern way to push and pop views.
1
2
3
4
5
NavigationStack {
NavigationLink("Go") {
Text("Next Screen")
}
}
22. What is Environment?
Answer: Environment allows you to share data globally across many views without passing it manually. It is useful for things like themes, settings, or system values.
1
@Environment(\.colorScheme) var colorScheme
23. What is ObservableObject?
Answer: ObservableObject is a class used to manage data that can change over time. When its data changes, SwiftUI automatically updates the UI.
1
2
3
class ViewModel: ObservableObject {
@Published var name = "John"
}
24. What is @ObservedObject?
Answer: @ObservedObject is used when a view depends on external data. It listens for changes and updates the UI automatically.
1
@ObservedObject var vm = ViewModel()
25. What is @StateObject?
Answer: @StateObject is used to create and manage a data source inside a view. It ensures the object is created only once and survives view updates.
1
@StateObject var vm = ViewModel()
26. What is @Published?
Answer: @Published is used inside ObservableObject. It tells SwiftUI to update UI whenever the value changes.
1
@Published var count = 0
27. What is LazyVStack?
Answer: LazyVStack loads views only when they appear on screen. This improves performance when working with large data.
1
2
3
4
5
6
7
ScrollView {
LazyVStack {
ForEach(0..<1000) { i in
Text("Item \(i)")
}
}
}
28. What is ScrollView?
Answer: ScrollView makes content scrollable vertically or horizontally. It is used when content does not fit on the screen.
1
2
3
ScrollView {
Text("Scrollable Content")
}
29. What is Sheet?
Answer: Sheet is used to present a modal view (popup screen). It appears from the bottom and is commonly used for forms or details.
1
2
3
.sheet(isPresented: $show) {
Text("Modal View")
}
30. What is onAppear?
Answer: onAppear runs code when the view appears on screen. It is useful for loading data or starting tasks.
1
2
3
.onAppear {
print("View loaded")
}
31. What is onDisappear?
Answer: onDisappear runs code when the view disappears. It is useful for cleanup or stopping tasks.
1
2
3
.onDisappear {
print("View removed")
}
32. What is Task in SwiftUI?
Answer: Task is used to run asynchronous code when a view loads. It works well with async/await.
1
2
3
.task {
await fetchData()
}
33. What is MVVM in SwiftUI?
Answer:
MVVM stands for Model-View-ViewModel.
- Model = data
- View = UI
- ViewModel = logic
It helps keep code clean and maintainable by separating UI and business logic.
34. What is Identifiable?
Answer: Identifiable is a protocol used to uniquely identify data items. SwiftUI uses it to efficiently update lists.
1
2
3
struct User: Identifiable {
let id = UUID()
}
35. What is GeometryReader?
Answer: GeometryReader gives access to layout size and position. It helps create responsive designs.
1
2
3
GeometryReader { geo in
Text("\(geo.size.width)")
}
36. What is Scene in SwiftUI?
Answer: Scene represents the app’s UI structure. It defines what appears when the app starts.
1
2
3
4
5
6
7
8
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
37. What is WindowGroup?
Answer: WindowGroup is the main container for app UI. It manages app windows and displays content.
38. Difference between State and Binding?
Answer:
State owns and manages data locally.
Binding shares and updates data from another view.
18. What is the main advantage of SwiftUI?
Answer: Less code, faster development, automatic UI updates, and cross-platform support.