Complete the code to make the struct conform to Equatable.
struct Point: Equatable {
var x: Int
var y: Int
static func ==(lhs: Point, rhs: Point) -> Bool {
return lhs.x [1] rhs.x && lhs.y == rhs.y
}
}The == operator checks if both x and y are equal for two points.
Complete the code to make the struct conform to Hashable by implementing hash(into:).
struct Person: Hashable {
var name: String
var age: Int
func hash(into hasher: inout Hasher) {
hasher.[1](name)
hasher.combine(age)
}
}The combine method adds the value to the hasher to create a hash.
Fix the error in the Comparable conformance by completing the less-than operator.
struct Rectangle: Comparable {
var width: Int
var height: Int
static func < (lhs: Rectangle, rhs: Rectangle) -> Bool {
return lhs.width [1] rhs.width
}
}The less-than operator < compares if the left width is smaller than the right width.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
let words = ["apple", "cat", "banana", "dog"] var lengths = [String: Int]() for word in words { if word.[1] > 3 { lengths[word] = word.[2] } }
The count property gives the number of characters in a string.
Fill all three blanks to create a sorted array of unique names using Set and sorted().
let names = ["Anna", "Bob", "Anna", "Charlie"] let uniqueSortedNames = Array([1](names)).[2]() [3] { $0 < $1 }
sorted() without by: when a closure is provided causes errors.First, convert the array to a Set to remove duplicates. Then use sorted(by:) with a closure to sort ascending. Finally, Array converts the set back to an array.