Swift: Set of Strings and Integers

Strings + Integers This demo shows one way of implementing a mix of types in a Set. Code Sample enum StringOrInt: Hashable, CustomStringConvertible { case string(String) case int(Int) var description: String { switch self { case .int(let int): return String(int) case .string(let string): return string } } } var mixedSet: Set<StringOrInt> = Set([.string("@lintuxt"), .int(0), .string("@lintuxt"), .int(1)]) print(mixedSet) /* Output: [0, 1, @lintuxt] */

May 28, 2024 · 1 min