Challenge - 5 Problems
Swift Conditional Conformance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about how conditional conformance allows Box to be Equatable only if T is Equatable.
✗ Incorrect
The Box struct conforms to Equatable only when its generic type T conforms to Equatable. Since Int is Equatable, Box is Equatable. box1 and box2 have the same value 5, so they are equal. box1 and box3 have different values, so they are not equal.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Check how the description property is used in the extension.
✗ Incorrect
The Wrapper struct conforms to CustomStringConvertible only if T does. Person conforms and returns its name as description. So Wrapper's description returns "Wrapper contains: Alice".
❓ Predict Output
advanced2: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") }
Attempts:
2 left
💡 Hint
Look at how Container encodes its content property.
✗ Incorrect
Container conforms to Codable only if T does. Item conforms to Codable, so Container- can be encoded. The JSON string represents the container with a content key holding the item data.
❓ Predict Output
advanced2: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))
Attempts:
2 left
💡 Hint
Check how Collection conformance enables contains method.
✗ Incorrect
MyArray conforms to Collection only if T is Equatable. Since Int is Equatable, MyArray is a Collection and has contains method. The array contains 2, so it prints true.
❓ Predict Output
expert3: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)
Attempts:
2 left
💡 Hint
Consider how Optional Equatable compares values and nil.
✗ Incorrect
Optional conforms to Equatable only if Wrapped does. Int is Equatable, so Optional is Equatable. Two optionals with same value are equal, optional with value and nil are not equal, and nil equals nil.