iOS Development SwiftUI Framework 1 — Questions and Answers
Question 1: What is SwiftUI's primary programming paradigm?
- Declarative UI (Correct answer)
- Imperative UI
- Event-driven UI
- Object-oriented UI
Correct answer: Declarative UI
SwiftUI uses a declarative syntax where you describe what the UI should look like for a given state, and the framework handles updates.
Question 2: Which SwiftUI property wrapper stores state local to a view?
- @State (Correct answer)
- @Binding
- @ObservedObject
- @Environment
Correct answer: @State
`@State` is a property wrapper that lets SwiftUI manage a value local to a view and re-render when it changes.
Question 3: What does `@Binding` do in SwiftUI?
- Creates a two-way connection to a state value owned by another view (Correct answer)
- Observes changes in an external object
- Reads a value from the environment
- Persists a value across app launches
Correct answer: Creates a two-way connection to a state value owned by another view
`@Binding` creates a reference to a `@State` value in another view, enabling child views to read and write that state.
Question 4: Which SwiftUI container stacks views vertically?
- VStack (Correct answer)
- HStack
- ZStack
- Group
Correct answer: VStack
`VStack` arranges its children in a vertical line, stacking views from top to bottom.
Question 5: What protocol must all SwiftUI views conform to?
- View (Correct answer)
- UIView
- Identifiable
- ObservableObject
Correct answer: View
Every SwiftUI view must conform to the `View` protocol and provide a `body` property that returns some view.
Question 6: What is the SwiftUI preview feature used for?
- Rendering a live view of a component inside Xcode without running the full app (Correct answer)
- Generating screenshots for the App Store
- Simulating network responses
- Testing view layouts on physical devices
Correct answer: Rendering a live view of a component inside Xcode without running the full app
SwiftUI Previews use the `#Preview` macro (or `PreviewProvider`) to render live, interactive views directly in Xcode's canvas.
What is SwiftUI's primary programming paradigm?