0
0
Swiftprogramming~20 mins

Conditional conformance in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Conditional Conformance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of conditional conformance with Equatable
What is the output of this Swift code using conditional conformance for Equatable?
Swift
struct Box<T> {
    let value: T
}
extension Box: Equatable where T: Equatable {
    static func == (lhs: Box<T>, rhs: Box<T>) -> Bool {
        return lhs.value == rhs.value
    }
}

let box1 = Box(value: 5)
let box2 = Box(value: 5)
let box3 = Box(value: 10)

print(box1 == box2)
print(box1 == box3)
Afalse\nfalse
Btrue\nfalse
Ctrue\ntrue
DCompilation error
Attempts:
2 left
💡 Hint
Think about how conditional conformance allows Box to be Equatable only if T is Equatable.
Predict Output
intermediate
2:00remaining
Output of conditional conformance with CustomStringConvertible
What will this Swift code print when using conditional conformance for CustomStringConvertible?
Swift
struct Wrapper<T> {
    let item: T
}
extension Wrapper: CustomStringConvertible where T: CustomStringConvertible {
    var description: String {
        return "Wrapper contains: \(item.description)"
    }
}

struct Person: CustomStringConvertible {
    let name: String
    var description: String { name }
}

let p = Person(name: "Alice")
let w = Wrapper(item: p)
print(w.description)
AWrapper contains: Alice
BWrapper contains: Person
CCompilation error
DWrapper contains: Optional("Alice")
Attempts:
2 left
💡 Hint
Check how the description property is used in the extension.
Predict Output
advanced
2:00remaining
Output of conditional conformance with Codable
What is the output of this Swift code that uses conditional conformance for Codable?
Swift
struct Container<T> {
    let content: T
}
extension Container: Codable where T: Codable {}

struct Item: Codable {
    let id: Int
    let name: String
}

let item = Item(id: 1, name: "Book")
let container = Container(content: item)

let encoder = JSONEncoder()
if let jsonData = try? encoder.encode(container),
   let jsonString = String(data: jsonData, encoding: .utf8) {
    print(jsonString)
} else {
    print("Encoding failed")
}
AEncoding failed
B{"id":1,"name":"Book"}
C{"content":{"id":1,"name":"Book"}}
DCompilation error
Attempts:
2 left
💡 Hint
Look at how Container encodes its content property.
Predict Output
advanced
2:00remaining
Output of conditional conformance with Sequence and Collection
What will this Swift code print when using conditional conformance for Collection?
Swift
struct MyArray<T> {
    var elements: [T]
}
extension MyArray: Collection where T: Equatable {
    typealias Index = Int
    var startIndex: Int { elements.startIndex }
    var endIndex: Int { elements.endIndex }
    subscript(position: Int) -> T { elements[position] }
    func index(after i: Int) -> Int { elements.index(after: i) }
}

let arr = MyArray(elements: [1, 2, 3])
print(arr.contains(2))
Atrue
BRuntime error
CCompilation error
Dfalse
Attempts:
2 left
💡 Hint
Check how Collection conformance enables contains method.
Predict Output
expert
3:00remaining
Output of nested conditional conformance with Optional and Equatable
What is the output of this Swift code using nested conditional conformance with Optional and Equatable?
Swift
extension Optional: Equatable where Wrapped: Equatable {
    public static func == (lhs: Optional<Wrapped>, rhs: Optional<Wrapped>) -> Bool {
        switch (lhs, rhs) {
        case (.some(let l), .some(let r)):
            return l == r
        case (.none, .none):
            return true
        default:
            return false
        }
    }
}

let a: Int? = 5
let b: Int? = 5
let c: Int? = nil

print(a == b)
print(a == c)
print(c == nil)
ACompilation error
Bfalse\nfalse\nfalse
Ctrue\ntrue\nfalse
Dtrue\nfalse\ntrue
Attempts:
2 left
💡 Hint
Consider how Optional Equatable compares values and nil.