Which statement about class instances assigned to two `let` constants is correct?
class Box { var n = 0 }
let x = Box()
let y = x
y.n = 42Read the full question →Swift is Apple's language for iOS and macOS. Reference-counting behaviour and value semantics drive most of its gotchas.
Retain cycles and capture lists, struct versus class copying, optionals and forced unwraps, protocol witness dispatch, and actor isolation in structured concurrency.
Where it shows up: iOS, iPadOS, macOS, watchOS engineering, and server-side Swift at a handful of companies.
Twenty real Swift questions from the library, code snippet included. Nothing here is paraphrased for search engines. It is the same text a signed-in user sees.
class Box { var n = 0 }
let x = Box()
let y = x
y.n = 42Read the full question →func check(_ n: Int?) -> String {
guard let n = n, n > 0 else { return "invalid" }
return "ok: \(n)"
}
print(check(0))Read the full question →let dict = ["a": 1]
let v = dict["b"]!
print(v)Read the full question →struct Counter { var value = 0 }
let c = Counter()
c.value = 1Read the full question →let value: Int? = 10
if let value = value {
print(value + 1)
}Read the full question →enum Reading: Int {
case low(Int)
case high(Int)
}Read the full question →var list: [Int]? = [1, 2, 3]
let r = list?.removeAll()
print(type(of: r))Read the full question →func read(_ name: String) -> String {
var status = "opened"
defer { status = "closed" }
return status
}Read the full question →let a: Set = [1, 2, 3]
let b: Set = [3, 4, 5]
let result = a.intersection(b)
print(result)Read the full question →let nums: [Int?] = [1, nil, 2, nil, 3]Read the full question →var dict = ["x": 1]
dict["x", default: 0] += 10Read the full question →var s: Set = [1, 2, 3]
s.insert(2)
s.insert(4)Read the full question →protocol Greeter {
func hello() -> String // a protocol requirement
}
extension Greeter {
func hello() -> String { "default hello" }
func bye() -> String { "default bye" } // NOT a requirement
}
struct G: Greeter {
func hello() -> String { "G hello" }
func bye() -> String { "G bye" }
}
let g: Greeter = G()
print(g.hello())
print(g.bye())Read the full question →protocol Container<Item> {
associatedtype Item
func get() -> Item
}Read the full question →@resultBuilder
struct Concat {
static func buildPartialBlock(first: String) -> String { first }
static func buildPartialBlock(accumulated: String, next: String) -> String {
accumulated + next
}
}
@Concat
func build() -> String {
"x"
"y"
"z"
}
let out = build()Read the full question →let scores: [String: Int] = ["math": 90]
print(scores["science"] ?? -1)Read the full question →protocol Shape { func area() -> Int }
extension Shape {
func area() -> Int { 0 }
func describe() -> String { "Shape area \(area())" }
}
struct Square: Shape {
func area() -> Int { 9 }
func describe() -> String { "Square!" }
}
let g: Shape = Square()
print(g.describe())Read the full question →protocol Greeter {
func hello()
}
extension Greeter {
func hello() { print("protocol") }
func bonus() { print("protocol bonus") }
}
struct Bob: Greeter {
func hello() { print("Bob") }
func bonus() { print("Bob bonus") }
}
let g: Greeter = Bob()
g.hello()
g.bonus()Read the full question →@resultBuilder
enum StringBuilder {
static func buildBlock(_ parts: String...) -> String { parts.joined() }
static func buildEither(first: String) -> String { first }
static func buildEither(second: String) -> String { second }
}
func make(@StringBuilder _ b: () -> String) -> String { b() }
let flag = false
print(make {
"a"
if flag { "b" } else { "c" }
})Read the full question →let count = 5
let result = count ?? 0Read the full question →We keep the correct answer, the explanation and the scoring behind sign-in so the platform stays honest, which is why the answer options and the worked explanation for every Swift question stay behind a free account. Signing in is free and takes a few seconds.
51 distinct Swift topics are tagged across the library. These are the real topic labels stored on the questions, not a hand-written list.
…