Swift Technology & Digital Applications 4 — Questions and Answers
Question 1: Which Swift property wrapper automatically publishes changes so SwiftUI views re-render?
- @State
- @Binding
- @Published (Correct answer)
- @Environment
Correct answer: @Published
`@Published` is a Combine property wrapper inside `ObservableObject` classes that emits through `objectWillChange` before each value change.
Question 2: What is the output of `[1, 2, 3].reduce(0, +)` in Swift?
- [1, 2, 3]
- 0
- 6 (Correct answer)
- 3
Correct answer: 6
`reduce(0, +)` starts with an initial value of 0 and adds each element, producing 0+1+2+3 = 6.
Question 3: In Swift, what distinguishes a `struct` from a `class` regarding memory management?
- Structs use automatic reference counting; classes do not
- Structs are value types copied on assignment; classes are reference types (Correct answer)
- Structs cannot have methods; classes can
- Structs are heap-allocated; classes are stack-allocated
Correct answer: Structs are value types copied on assignment; classes are reference types
Structs are value types, so assignment and function argument passing produce independent copies, while classes share a single reference.
Question 4: Which Swift error-handling mechanism is used to propagate errors without using `throw`?
- Result<Success, Failure> (Correct answer)
- Optional chaining
- defer
- guard let
Correct answer: Result<Success, Failure>
`Result` lets functions return either a success value or an error without throwing, useful in callbacks and Combine pipelines.
Question 5: What does the `@objc` attribute enable in Swift?
- It allows a Swift declaration to be visible and used from Objective-C code (Correct answer)
- It forces a class to use manual retain-release memory management
- It marks a method as thread-safe
- It prevents subclassing of a Swift class
Correct answer: It allows a Swift declaration to be visible and used from Objective-C code
`@objc` exposes Swift declarations to the Objective-C runtime, enabling interoperability with Objective-C frameworks and dynamic dispatch features.
Question 6: In SwiftUI, what is the purpose of `GeometryReader`?
- It reads data from Core Location
- It provides its child views with the size and coordinate space of the parent (Correct answer)
- It generates random geometric shapes
- It handles touch gesture coordinates
Correct answer: It provides its child views with the size and coordinate space of the parent
`GeometryReader` is a container view that exposes a `GeometryProxy` to child views, giving them access to the parent's size and coordinate space.
Question 7: Which Swift feature allows you to add new methods to an existing type without subclassing?
- Generics
- Extensions (Correct answer)
- Protocols
- Enumerations
Correct answer: Extensions
Extensions let you add computed properties, methods, initializers, and protocol conformances to any existing type, including system types.
Which Swift property wrapper automatically publishes changes so SwiftUI views re-render?