The Equatable protocol allows you to check if two values are equal using the == operator. It helps compare if two instances are the same.
Click to reveal answer
beginner
Why is the Hashable protocol important?
The Hashable protocol lets a type be used in collections like Set or as keys in Dictionary. It provides a way to create a hash value for each instance.
Click to reveal answer
beginner
What does the Comparable protocol allow you to do?
The Comparable protocol lets you compare values to see which is smaller, equal, or larger using operators like <, <=, >, and >=. It helps sort values.
Click to reveal answer
intermediate
How do you make a custom Swift struct conform to Equatable?
You add Equatable to the struct declaration. Swift can automatically create the == function if all properties are Equatable. Example: struct Point: Equatable { var x: Int; var y: Int }
Click to reveal answer
intermediate
What must you implement when conforming to Hashable?
You must provide a hash(into:) method that feeds the essential properties into the hasher. This creates a hash value for the instance.
Click to reveal answer
Which operator is required by the Equatable protocol?
A==
B+
C<
D!=
✗ Incorrect
The Equatable protocol requires the == operator to check equality.
What does conforming to Hashable enable for a type?
AUse in sets and dictionary keys
BAutomatic sorting
CPrinting to console
DInheritance from other classes
✗ Incorrect
Conforming to Hashable allows a type to be stored in sets and used as dictionary keys.
Which protocol lets you compare if one value is less than another?
AEquatable
BHashable
CCodable
DComparable
✗ Incorrect
Comparable provides operators like < to compare values.
If a struct has only Equatable properties, what happens when you add Equatable conformance?
AYou must write the <code>==</code> function manually
BSwift automatically generates the <code>==</code> function
CThe struct cannot conform to Equatable
DYou must implement <code>hash(into:)</code>
✗ Incorrect
Swift can automatically create the == function if all properties are Equatable.
What method must you implement for Hashable conformance?
Afunc equals()
Bfunc compare(to other: Self)
Cfunc hash(into hasher: inout Hasher)
Dfunc encode()
✗ Incorrect
You must implement hash(into:) to provide a hash value.
Explain the purpose of the Equatable, Hashable, and Comparable protocols in Swift and how they differ.
Think about equality, uniqueness, and order.
You got /4 concepts.
Describe how you would make a custom struct conform to Equatable and Hashable in Swift.
Focus on protocol conformance and required methods.
You got /4 concepts.
Practice
(1/5)
1.
Which protocol in Swift allows you to check if two instances of a type are equal using ==?
easy
A. Hashable
B. Codable
C. Comparable
D. Equatable
Solution
Step 1: Understand the purpose of Equatable
Equatable protocol allows comparing two instances for equality using ==.
Step 2: Compare with other protocols
Hashable is for hashing, Comparable is for ordering, Codable is for encoding/decoding.
Final Answer:
Equatable -> Option D
Quick Check:
Equality check = Equatable [OK]
Hint: Equality means Equatable protocol [OK]
Common Mistakes:
Confusing Hashable with Equatable
Thinking Comparable checks equality
Mixing Codable with Equatable
2.
Which of the following is the correct way to declare a struct Person that conforms to Equatable and Hashable?
struct Person: ??? {
var name: String
var age: Int
}
easy
A. Equatable, Hashable
B. Comparable, Hashable
C. Equatable, Comparable
D. Codable, Hashable
Solution
Step 1: Identify needed protocols
To compare equality and use in sets/dictionaries, use Equatable and Hashable.
Step 2: Check syntax for protocol conformance
Protocols are listed separated by commas after colon in struct declaration.
Final Answer:
Equatable, Hashable -> Option A
Quick Check:
Equality + hashing = Equatable, Hashable [OK]
Hint: List protocols comma-separated after colon [OK]
Common Mistakes:
Using Comparable instead of Equatable
Forgetting commas between protocols
Using Codable instead of Equatable
3.
What will be the output of the following Swift code?
struct Item: Comparable {
var value: Int
static func < (lhs: Item, rhs: Item) -> Bool {
return lhs.value < rhs.value
}
}
let items = [Item(value: 3), Item(value: 1), Item(value: 2)]
let sortedValues = items.sorted().map { $0.value }
print(sortedValues)
medium
A. [3, 2, 1]
B. [1, 2, 3]
C. [2, 3, 1]
D. Compilation error
Solution
Step 1: Understand Comparable conformance
The < operator compares value properties to sort ascending.
Step 2: Analyze sorting and mapping
Sorting the array orders items by value: 1, 2, 3. Mapping extracts values in this order.
Final Answer:
[1, 2, 3] -> Option B
Quick Check:
Sorted values ascending = [1, 2, 3] [OK]
Hint: Comparable sorts ascending by < operator [OK]
Common Mistakes:
Assuming descending order by default
Confusing map output with original array
Forgetting to implement < operator
4.
Identify the error in this Swift struct that tries to conform to Hashable:
struct Book: Hashable {
var title: String
var pages: Int
func hash(into hasher: inout Hasher) {
hasher.combine(title)
}
}
medium
A. Missing combine call for all properties in hash(into:)
B. Struct cannot conform to Hashable without Codable
C. Incorrect method signature for hash(into:)
D. Missing == operator implementation for Equatable
Solution
Step 1: Check Hashable requirements
Hashable requires hashing all properties that affect equality.
Step 2: Analyze hash(into:) implementation
Only title is combined; pages is missing, causing incorrect hashing.
Final Answer:
Missing combine call for all properties in hash(into:) -> Option A
Quick Check:
Hash all relevant properties [OK]
Hint: Hash all properties used in equality [OK]
Common Mistakes:
Hashing only some properties
Forgetting to implement == operator if needed
Wrong method signature for hash(into:)
5.
You want to create a struct Player that can be stored in a Set, compared for equality, and sorted by score descending. Which protocols should Player conform to, and how should you implement the sorting?
hard
A. Conform to Equatable and Hashable; implement < to compare scores descending
B. Conform to Comparable only; implement < to compare scores ascending
C. Conform to Equatable, Hashable, Comparable; implement < to compare scores descending
D. Conform to Hashable only; sorting is automatic
Solution
Step 1: Identify needed protocols
To store in Set and check equality, conform to Equatable and Hashable. To sort, conform to Comparable.
Step 2: Implement sorting logic
Implement < operator to compare scores descending by reversing comparison.
Final Answer:
Conform to Equatable, Hashable, Comparable; implement < for descending scores -> Option C