Hello. I makes struct Bundle with custom Equatable behavior. And ideally, I want to in the final set must be only two items ('one'(or 'two' or 'three') and 'four'), but in several runs the final set contains different count of items. What I do wrong? And what hashable function is need to be?
// Forum
public struct Forum {
public init() {}
struct Bundle: Equatable, Hashable, CustomStringConvertible {
var description: String { return name }
let name: String
let personOne: String
let personTwo: String
public static func == (lhs: Bundle, rhs: Bundle) -> Bool {
var result = false
if lhs.personOne == rhs.personOne && lhs.personTwo == rhs.personTwo { result = true }
if lhs.personOne == rhs.personTwo && lhs.personTwo == rhs.personOne { result = true }
return result
}
public init(first: String, second: String, name: String) {
personOne = first
personTwo = second
self.name = name
}
// func hash(into hasher: inout Hasher) {
// hasher.combine(???)
// }
}
public func work() {
let one = Bundle(first: "10", second: "3", name: "one")
let two = Bundle(first: "3", second: "10", name: "two")
let three = Bundle(first: "10", second: "3", name: "three")
let four = Bundle(first: "5", second: "3", name: "four")
var set = Set<Bundle>()
set.insert(one)
set.insert(two)
set.insert(three)
set.insert(four)
print(set)
// [two, one, three, four]
// [one, four, three]
// [two, four, three, one]
// [four, one, two]
// [four, three, two, one]
// [two, four, one]
// [one, four]
}
}