Swift: Pattern Matching

Disclaimers I’m on the path of learning these complex topics, so if you find a mistake, please let me know at @lintuxt The ideas, concepts, stories, and examples of this post are ALL a product of my invention. Any similarities with reality are just a pure coincidence. The post assumes some basic knowledge about if/else and switch statements in Swift. Pattern Matching in Swift Pattern Matching is one of the flagships of the Functional Programming paradigm, and Swift has you covered....

May 28, 2024 · 5 min

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