Swift (Mobile Dev) 5 — Questions and Answers
Question 1: What does the `Codable` typealias combine in Swift?
- Hashable and Equatable
- Encodable and Decodable (Correct answer)
- Comparable and Identifiable
- Serializable and Parseable
Correct answer: Encodable and Decodable
`Codable` is a type alias for `Encodable & Decodable`, enabling both JSON serialization and deserialization.
Question 2: In Swift's structured concurrency, what is the role of a `TaskGroup`?
- It serializes async tasks on the main actor
- It creates child tasks that run concurrently and collects their results (Correct answer)
- It cancels all sibling tasks when one throws
- It replaces DispatchGroup entirely
Correct answer: It creates child tasks that run concurrently and collects their results
TaskGroup lets you spawn a dynamic number of child tasks that run concurrently and then gather their results in order.
Question 3: Which Xcode build setting controls the minimum iOS version your app can run on?
- SWIFT_VERSION
- IPHONEOS_DEPLOYMENT_TARGET (Correct answer)
- TARGETED_DEVICE_FAMILY
- PRODUCT_BUNDLE_IDENTIFIER
Correct answer: IPHONEOS_DEPLOYMENT_TARGET
IPHONEOS_DEPLOYMENT_TARGET sets the lowest iOS version the binary supports, affecting which APIs are available.
Question 4: What is a Swift `protocol` with an associated type commonly used for?
- Defining a concrete class hierarchy
- Creating generic abstractions that work with multiple conforming types (Correct answer)
- Marking types as thread-safe
- Replacing enums with polymorphism
Correct answer: Creating generic abstractions that work with multiple conforming types
Associated types make protocols generic, letting conforming types specify the concrete type while the protocol defines the interface.
Question 5: Which method on `UserDefaults` persists a String value for a given key?
- save(_:forKey:)
- set(_:forKey:) (Correct answer)
- write(_:toKey:)
- store(_:key:)
Correct answer: set(_:forKey:)
UserDefaults.standard.set(_:forKey:) stores the value persistently in the app's defaults database.
Question 6: In Swift, what happens when you call `map` on an Optional that contains a value?
- It throws a runtime error
- It returns nil
- It applies the transform and returns the result wrapped in Optional (Correct answer)
- It unwraps the value automatically
Correct answer: It applies the transform and returns the result wrapped in Optional
Optional.map applies the provided closure to the wrapped value if present and returns Optional(result), otherwise returns nil.
Question 7: Which SwiftUI modifier clips a view's content to a rounded rectangle shape?
- .border(RoundedRectangle(cornerRadius:))
- .clipShape(RoundedRectangle(cornerRadius:)) (Correct answer)
- .mask(RoundedRectangle(cornerRadius:))
- .cornerRadius applied to a shape
Correct answer: .clipShape(RoundedRectangle(cornerRadius:))
.clipShape(RoundedRectangle(cornerRadius:)) masks the view's rendering to the specified rounded rectangle boundary.
What does the `Codable` typealias combine in Swift?