๐Ÿ€generics

Swift โŸฉ type โŸฉ category โŸฉ generics

Also called generic types, code that works for multiple types.

// (automatically synthesized Hashable)
struct Pair<T: Hashable, U: Hashable>: Hashable {

    let left: T
    let right: U
    
    // convenience init
    init(_ left: T, _ right: U){
        self.left = left
        self.right = right
    }
}

// โญ๏ธ ไฝฟ็”จ Pair ๆ™‚๏ผŒไธ้œ€็‰นๅˆฅๆŒ‡ๅฎš <T, U>
let pair = Pair("Joe", 20)      
let dict = [pair: "friend"]

Last updated