Adding values of my own type to a Set

suggest change

To put your own type in a Set, that type must conform to Hashable.

struct Starship: Hashable {
    let name: String
    func hash(into hasher: inout Hasher) { hasher.combine(name) }
}

func ==(left:Starship, right: Starship) -> Bool {
    return left.name == right.name
}

Now you can create a Set of Starship(s)

let ships : Set<Starship> = [Starship(name:"Enterprise D"), Starship(name:"Voyager"), Starship(name:"Defiant") ]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents