SwiftUI Developer Assessment — Questions and Answers
Question 1: Which modifier continuously animates a view with a given animation as long as it is visible?
- .animation(.linear.repeatForever(), value: someValue)
- Animating a never-changing value using .repeatForever()
- .alwaysAnimate()
- Both approaches can work (Correct answer)
Correct answer: Both approaches can work
You can animate a never-changing value (like a constant) with `.repeatForever()`, or use a state toggle — both are valid techniques for looping animations.
Question 2: What is the `@Namespace` property wrapper used for in SwiftUI?
- Defining matched geometry namespaces for hero animations (Correct answer)
- Storing animation references
- Scoping state variables
- Creating view namespaces to avoid name collisions
Correct answer: Defining matched geometry namespaces for hero animations
`@Namespace` creates an animation namespace used with `.matchedGeometryEffect()` to animate shared geometry between views.
Question 3: What does the `$` prefix do when used with a `@State` property?
- Projects a Binding to the state (Correct answer)
- Forces an immediate view update
- Reads the wrapped value
- Accesses the publisher
Correct answer: Projects a Binding to the state
Prefixing a `@State` property with `$` accesses its projected value, which is a `Binding` that child views can use to read and write the state.
Question 4: How do you pass an `@EnvironmentObject` to a child view?
- Use @Binding in the child
- Use `.environmentObject()` modifier on the parent view (Correct answer)
- Pass it as an init parameter
- Declare it with @State in the parent
Correct answer: Use `.environmentObject()` modifier on the parent view
You inject the object into the environment using `.environmentObject()`, making it available to all descendant views.
Question 5: What does `@Binding` do in SwiftUI?
- Reads environment values
- Observes changes in an ObservableObject
- Creates a two-way connection to a state owned by another view (Correct answer)
- Creates a new state variable
Correct answer: Creates a two-way connection to a state owned by another view
`@Binding` provides a reference to state stored elsewhere, allowing a child view to read and write it.
Question 6: What is the purpose of the `@GestureState` property wrapper in SwiftUI?
- To define the initial state of a gesture
- To store transient gesture values that reset when the gesture ends (Correct answer)
- To track gesture history
- To persist gesture data across app sessions
Correct answer: To store transient gesture values that reset when the gesture ends
@GestureState automatically resets to its initial value when the gesture ends, making it ideal for transient interaction state.
Question 7: Which modifier adds a button to the navigation bar's trailing edge?
- .toolbar { ToolbarItem(placement: .navigationBarTrailing) { ... } }
- .navigationBarItems(trailing:)
- Both A and B work (Correct answer)
- .trailingButton()
Correct answer: Both A and B work
Both `.toolbar` with `ToolbarItem` (preferred in iOS 14+) and the older `.navigationBarItems(trailing:)` can add trailing bar buttons.
Question 8: What is the purpose of `@AppStorage` in SwiftUI?
- Manages in-memory app-wide state
- Provides a binding to a UserDefaults key (Correct answer)
- Stores data in iCloud
- Persists state across scene sessions
Correct answer: Provides a binding to a UserDefaults key
`@AppStorage` wraps UserDefaults, automatically updating the view when the stored value changes.
Question 9: What view implements a tab bar in SwiftUI?
- NavigationStack
- SegmentedView
- TabView (Correct answer)
- PageView
Correct answer: TabView
`TabView` renders a tab bar at the bottom and switches between child views based on the selected tab.
Question 10: What does marking a property with `@Published` inside an ObservableObject do?
- Makes the property read-only
- Marks the property as thread-safe
- Persists the property to UserDefaults
- Automatically notifies subscribers when the property changes (Correct answer)
Correct answer: Automatically notifies subscribers when the property changes
`@Published` uses Combine to emit a change event whenever the property's value is set, triggering view re-renders.
Question 11: Which view provides a navigation stack with a back button in SwiftUI (iOS 16+)?
- NavigationStack (Correct answer)
- NavigationView
- TabView
- NavigationSplitView
Correct answer: NavigationStack
`NavigationStack` replaced `NavigationView` in iOS 16 as the preferred stack-based navigation container.
Question 12: What property wrapper is used to declare local mutable state in a SwiftUI view?
- @EnvironmentObject
- @ObservedObject
- @Binding
- @State (Correct answer)
Correct answer: @State
`@State` declares a source of truth owned by the view for simple value types.
Question 13: How do you create a repeating animation in SwiftUI?
- .animation(.linear.loop())
- .animation(.linear.repeatForever()) (Correct answer)
- .repeatAnimation()
- .loop(animation:)
Correct answer: .animation(.linear.repeatForever())
Chaining `.repeatForever(autoreverses:)` onto an animation makes it repeat indefinitely.
Question 14: How do you apply a linear gradient as a fill in SwiftUI?
- .fill(LinearGradient(colors:startPoint:endPoint:))
- .background(LinearGradient(...))
- .gradient(.linear)
- Both A and C work (Correct answer)
Correct answer: Both A and C work
You can apply a LinearGradient via `.fill()` on a Shape or via `.background()` on any view.
Question 15: What does the `minimumDistance` parameter in DragGesture control?
- The maximum allowed drag length
- The distance from screen edge required
- How far the user must move before the gesture is recognized (Correct answer)
- The velocity threshold for the gesture
Correct answer: How far the user must move before the gesture is recognized
minimumDistance sets the threshold in points the user must drag before DragGesture begins recognizing the gesture.
Question 16: What will the code below yield as a result?
- nill
- Nothing will be output
- 0
- 1 (Correct answer)
Correct answer: 1
The variable `x` is initialized to `0`. The `if` condition `x == 0` evaluates to true, so the code inside its block is executed, setting `x` to `1`. The `else if` condition is then skipped because the first `if` condition was met. The program proceeds to print the final value of `x`, which is `1`.
Question 17: Which of the following is an incorrect Swift value type?
- Class
- Double
- Enum
- Character (Correct answer)
Correct answer: Character
The question asks to identify an 'incorrect Swift value type.' In Swift, `Character` is fundamentally a value type, as it is implemented as a struct, meaning copies are made when assigned or passed. However, if `Character` is the intended correct answer, it suggests a non-standard interpretation or a potential flaw in the question's premise. Typically, `Class` is the only reference type among the given options, making it the one that is *not* a value type in standard Swift.
Question 18: Which closure on a SwiftUI gesture fires exactly once when the user completes the gesture?
- .onEnded { } (Correct answer)
- .onDone { }
- .onFinished { }
- .onCompleted { }
Correct answer: .onEnded { }
.onEnded { } fires once when the gesture finishes, receiving the final gesture value at that moment.
Question 19: Which modifier applies an animation phase in the new Keyframe Animator (iOS 17)?
- .sequenceAnimation()
- .animationPhase()
- .keyframeAnimator(initialValue:keyframes:content:) (Correct answer)
- .phaseAnimator()
Correct answer: .keyframeAnimator(initialValue:keyframes:content:)
`KeyframeAnimator` (iOS 17) lets you define multi-property keyframe tracks to animate complex, choreographed sequences.
Question 20: What modifier is used to set the background color of a SwiftUI view?
- .background() (Correct answer)
- .fill()
- .backgroundColor()
- .foregroundColor()
Correct answer: .background()
The `.background()` modifier applies a background color or view behind the modified view.
Question 21: How do you animate only specific properties of a view in SwiftUI?
- Use AnimatableModifier for each property
- Apply .animation(_:value:) to scope it to a specific value
- Use withAnimation and all properties animate
- Both B and C can scope animation to specific properties (Correct answer)
Correct answer: Both B and C can scope animation to specific properties
You can scope animations with `.animation(_:value:)` for simple cases, or implement `AnimatableModifier` for fully custom animatable data.
Question 22: What does `.navigationBarHidden(true)` do in SwiftUI?
- Hides all toolbars
- Removes the back button only
- Hides the status bar
- Hides the navigation bar for the current view (Correct answer)
Correct answer: Hides the navigation bar for the current view
`.navigationBarHidden(true)` hides the navigation bar when the view is presented inside a NavigationStack.
Question 23: What will the code below yield as a result?
- 1, 5, 9, 13, 17
- 1, 4, 8, 12, 16
- 4, 8, 12, 16
- 1, 5, 9, 13 (Correct answer)
Correct answer: 1, 5, 9, 13
The `stride(from:to:by:)` function generates a sequence of values starting from the `from` value, incrementing by the `by` value, and stopping *before* reaching the `to` value. Starting at 1 and incrementing by 4, the sequence is 1, 5, 9, 13. The next value, 17, is not included because the stride stops *before* 17.
Question 24: What data type will be allocated to result in the code below?
- Int
- UInt
- Array
- Tuple (Correct answer)
Correct answer: Tuple
In Swift, a tuple is a compound data type that groups multiple values into a single, ordered collection. The code `let result = (true, "hello", 10)` creates a tuple because it combines values of different types (`Bool`, `String`, `Int`) within parentheses. The `result` constant will therefore be allocated as a tuple of type `(Bool, String, Int)`.
Question 25: What protocol must a custom type conform to in order for SwiftUI to interpolate it during animations?
- VectorArithmetic
- Codable
- Animatable (Correct answer)
- Equatable
Correct answer: Animatable
SwiftUI uses the `Animatable` protocol (which requires `animatableData` of type `VectorArithmetic`) to interpolate custom types during animations.
Question 26: Which view acts as a flexible spacer that expands to fill available space in a stack?
- EmptyView
- Rectangle
- Divider
- Spacer (Correct answer)
Correct answer: Spacer
Spacer expands to fill all available space along the stack's primary axis.
Question 27: What is a side effect of marking a class property `@Published` when its setter is called on a background thread?
- It queues the update for the next run loop
- It crashes immediately
- It may cause undefined view update behavior since UI must be updated on the main thread (Correct answer)
- Nothing — it is thread-safe automatically
Correct answer: It may cause undefined view update behavior since UI must be updated on the main thread
SwiftUI view updates must occur on the main thread; publishing on a background thread can cause runtime warnings or undefined behavior.
Question 28: What is the default animation used when you call `withAnimation {}` with no parameters?
- .linear
- .easeIn
- .easeInOut with 0.35s duration (Correct answer)
- .spring()
Correct answer: .easeInOut with 0.35s duration
When called without arguments, `withAnimation` uses the default ease-in-out animation with approximately a 0.35 second duration.
Question 29: Which framework is the reactive foundation underlying SwiftUI's data flow?
- PromiseKit
- RxSwift
- Combine (Correct answer)
- Foundation
Correct answer: Combine
SwiftUI's property wrappers like `@Published` and `@ObservedObject` are built on Apple's Combine framework.
Question 30: How do you add a section with a header to a SwiftUI List?
- List(header:)
- Group(header:)
- Section(header:) { rows } (Correct answer)
- .listHeader()
Correct answer: Section(header:) { rows }
Wrap rows in a `Section` view with a `header` parameter to create grouped sections inside a List.
Question 31: _______ is used to create mutable objects.
- None of the above
- Let
- Let and Var
- Var (Correct answer)
Correct answer: Var
In Swift, the `var` keyword is used to declare variables whose values can be changed or mutated after their initial assignment. This makes them mutable objects. In contrast, `let` declares constants, whose values cannot be altered once set, making them immutable.
Question 32: Which SwiftUI view renders a scrollable list of rows?
- ScrollView
- Table
- List (Correct answer)
- LazyVStack
Correct answer: List
`List` renders a platform-styled scrollable list with built-in support for separators, selection, and swipe actions.
Question 33: Which built-in transition slides a view in from the leading edge?
- .move(edge: .leading) (Correct answer)
- .offset()
- .slide
- .push(from: .leading)
Correct answer: .move(edge: .leading)
`.move(edge: .leading)` slides a view in from or out to the leading edge of the screen.
Question 34: What does `LazyVStack` do differently compared to `VStack`?
- It supports horizontal scrolling
- It only renders views as they come on screen (Correct answer)
- It supports sections automatically
- It caches views off-screen
Correct answer: It only renders views as they come on screen
LazyVStack defers creating views until they are about to be visible, improving performance for large datasets.
Question 35: What function do you call to animate a state change in SwiftUI?
- withAnimation() (Correct answer)
- transition()
- animate()
- Animation.run()
Correct answer: withAnimation()
Wrapping a state change in `withAnimation {}` causes SwiftUI to animate any resulting view changes.
Question 36: How can a presented sheet dismiss itself in SwiftUI?
- Set isPresented to false directly
- Use @Environment(\.dismiss) and call dismiss()
- Call dismiss() on the view
- Both B and C work (Correct answer)
Correct answer: Both B and C work
You can dismiss by using `@Environment(\.dismiss)` to call `dismiss()`, or by toggling the `isPresented` binding to `false`.
Question 37: What happens to a `@State` variable when a SwiftUI view is destroyed and recreated?
- It triggers an animation
- It retains its value
- It is saved to disk automatically
- It resets to its initial value (Correct answer)
Correct answer: It resets to its initial value
SwiftUI resets `@State` to its initial value each time the view's identity changes and it is recreated.
Question 38: What is `NavigationPath` used for in iOS 16+ SwiftUI navigation?
- Caching previous view states
- Animating navigation transitions
- Defining URL routes for the app
- A type-erased list of navigation destinations enabling programmatic stack control (Correct answer)
Correct answer: A type-erased list of navigation destinations enabling programmatic stack control
`NavigationPath` is a type-erased collection you bind to a `NavigationStack` to push, pop, or jump to any destination programmatically.
Question 39: Which modifier applies a hue rotation effect to all colors in a SwiftUI view?
- .hueRotation(_:) (Correct answer)
- .contrast()
- .colorInvert()
- .saturation()
Correct answer: .hueRotation(_:)
`.hueRotation(_:)` shifts the hue of all colors in the view by the given angle, cycling through the color wheel.
Question 40: Which property wrapper provides edit mode state to a SwiftUI List?
- @State
- @ObservedObject
- @Binding
- @Environment(\.editMode) (Correct answer)
Correct answer: @Environment(\.editMode)
`@Environment(\.editMode)` reads and writes the current edit mode, enabling selection and reorder controls in a List.
Question 41: What does the `.onChange(of:perform:)` modifier do?
- Triggers a closure whenever a specified value changes (Correct answer)
- Animates a value change
- Prevents view updates
- Debounces rapid changes
Correct answer: Triggers a closure whenever a specified value changes
`.onChange(of:perform:)` observes a value and calls the closure with the new value each time it changes.
Question 42: Which property of RotationGesture.Value gives the angle of rotation?
- rotation (Correct answer)
- angle
- degrees
- radians
Correct answer: rotation
The rotation property of RotationGesture.Value returns an Angle struct representing how far the gesture has rotated.
Question 43: Which alignment option centers content both horizontally and vertically in a ZStack?
- .leading
- .topLeading
- .bottomTrailing
- .center (Correct answer)
Correct answer: .center
ZStack's default alignment is `.center`, which centers children both horizontally and vertically.
Question 44: Which animation creates a spring effect in SwiftUI?
- .linear
- .spring() (Correct answer)
- .interpolatingSpring()
- .easeInOut
Correct answer: .spring()
`.spring()` applies a spring-based animation with configurable response, dampingFraction, and blendDuration.
Question 45: How do you programmatically select a tab in SwiftUI's `TabView`?
- Use NavigationStack inside TabView
- Call tabView.select(tag:)
- Use `TabView(selection:)` with a binding and `.tag()` on each tab (Correct answer)
- Use @EnvironmentObject to share selection
Correct answer: Use `TabView(selection:)` with a binding and `.tag()` on each tab
Bind a state variable to `TabView(selection:)` and tag each tab view with `.tag()` to enable programmatic tab selection.
Question 46: Which modifier adds a drop shadow to a SwiftUI view?
- .shadow() (Correct answer)
- .shade()
- .dropShadow()
- .elevation()
Correct answer: .shadow()
The `.shadow(color:radius:x:y:)` modifier renders a shadow behind the view.
Question 47: How do you add a custom transition using `AnyTransition` in SwiftUI?
- Subclass Animation
- Use withAnimation and custom offsets only
- Use AnyTransition.modifier(active:identity:) (Correct answer)
- Implement the Transition protocol
Correct answer: Use AnyTransition.modifier(active:identity:)
`AnyTransition.modifier(active:identity:)` lets you define a custom transition using ViewModifier states for the active and identity states.
Question 48: Which SwiftUI modifier is used to add a tap gesture to a view?
- .onTapGesture { } (Correct answer)
- .tapGesture { }
- .addTap { }
- .gesture(TapRecognizer())
Correct answer: .onTapGesture { }
.onTapGesture { } is the SwiftUI modifier that attaches a tap gesture action to a view.
Question 49: What is `@SceneStorage` used for in SwiftUI?
- Persisting lightweight UI state per scene instance (Correct answer)
- Storing large data objects
- Sharing state across all app scenes
- Syncing data with CloudKit
Correct answer: Persisting lightweight UI state per scene instance
`@SceneStorage` persists small amounts of UI state tied to a specific scene, restoring it when the scene is recreated.
Question 50: What does the `delay` parameter on an animation do in SwiftUI?
- Schedules a view update
- Slows the animation playback rate
- Delays the start of the animation by the given time interval (Correct answer)
- Pauses the app for that duration
Correct answer: Delays the start of the animation by the given time interval
Calling `.delay()` on an animation causes it to wait the specified number of seconds before starting.
Question 51: How do you conditionally show a view in SwiftUI?
- show(view, when: condition)
- if condition { view } (Correct answer)
- view.opacity(condition ? 1 : 0) only
- view.visible(condition)
Correct answer: if condition { view }
You use a standard Swift `if` statement inside a `@ViewBuilder` body to conditionally include views.
Question 52: How does SwiftUI determine when to re-render a view?
- It re-renders only when explicitly called
- It re-renders when its input state or bindings change (Correct answer)
- It re-renders on every timer tick
- It re-renders on every user interaction
Correct answer: It re-renders when its input state or bindings change
SwiftUI's diffing engine re-evaluates and re-renders a view only when its declared dependencies (state, bindings, environment) change.
Question 53: Which Swift 5.9+ macro simplifies creating an ObservableObject without `@Published` annotations?
- @Model
- @State
- @Observed
- @Observable (Correct answer)
Correct answer: @Observable
The `@Observable` macro (introduced in Swift 5.9 / iOS 17) automatically tracks property access and synthesizes observation without `@Published`.
Question 54: How do you create an on/off toggle in SwiftUI?
- Switch(isOn: $flag)
- Toggle("Label", isOn: $flag) (Correct answer)
- Toggle(isOn: $flag, label: { Text("Label") })
- Checkbox(isChecked: $flag)
Correct answer: Toggle("Label", isOn: $flag)
`Toggle` takes a label string and an `isOn` Binding<Bool>, rendering as a system toggle switch.
Question 55: What does the `.transition()` modifier control in SwiftUI?
- How a view animates continuously
- The animation curve applied to a view
- How a view enters and exits the view hierarchy (Correct answer)
- The sequence of animations
Correct answer: How a view enters and exits the view hierarchy
`.transition()` defines the animation applied when a view is inserted into or removed from the view hierarchy.
Question 56: Which view in SwiftUI is designed for data-entry forms?
- List
- VStack
- Form (Correct answer)
- ScrollView
Correct answer: Form
`Form` renders an adaptive grouped-list layout optimized for user input, automatically styling controls like Toggle and Picker.
Question 57: What property of DragGesture.Value provides the offset from the start of the drag?
- translation (Correct answer)
- distance
- delta
- offset
Correct answer: translation
The translation property of DragGesture.Value is a CGSize representing the total movement from the drag start.
Question 58: How do you present a modal sheet in SwiftUI?
- .overlay(isPresented:)
- .modal(isPresented:content:)
- .sheet(isPresented:content:) (Correct answer)
- .present()
Correct answer: .sheet(isPresented:content:)
The `.sheet(isPresented:content:)` modifier presents a modal sheet when the binding's value becomes true.
Question 59: Which property wrapper reads a value from SwiftUI's environment without requiring it to be an ObservableObject?
- @Environment (Correct answer)
- @SceneStorage
- @EnvironmentObject
- @AppStorage
Correct answer: @Environment
`@Environment` reads values from the environment using a key path, such as `\.colorScheme` or `\.locale`.
Question 60: What does `.phaseAnimator(_:content:animation:)` do in SwiftUI (iOS 17)?
- Applies physics simulation
- Syncs animation to audio phases
- Cycles through a sequence of phases, animating between them (Correct answer)
- Runs an animation in a separate thread
Correct answer: Cycles through a sequence of phases, animating between them
`PhaseAnimator` cycles through a sequence of states, automatically animating between each phase in a loop.
SwiftUI Developer Assessment
SwiftUI is Apple's declarative UI framework for building apps across all Apple platforms. This assessment tests proficiency in building user interfaces, managing state and data flow, handling navigation, animations, gestures, and custom drawing using SwiftUI.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds