I reviewed it many times and felt that I didn't really think about it, so I will write it as a memorandum.
You can use Equtable
to compare objects in Swift
.
For example, I think it is effective when you give two arguments to a structure and want to compare with only one argument.
・ I want to judge a blue apple and a red apple as the same apple. ・ The information given is the name and color.
//Equatable ensures that objects can be compared
struct Fruit: Equatable {
var name: String
var color: String
//When you want to get the result by judging only the name
static func == (lhs: Fruit, rhs: Fruit) -> Bool {
return lhs.name == rhs.name
}
}
let gApple = Fruit(name: "Apple", color: "green")
let rApple = Fruit(name: "Apple", color: "red")
var result = gApple == rApple
print(result) // output: true
It's pretty rough, but I'd like to add it if there is a specific use.
Recommended Posts