Make a Swift dictionary where the key is “Type”?

后端 未结 3 1728
忘了有多久
忘了有多久 2020-11-29 07:29

I\'m trying to do this sort of thing ..

static var recycle: [Type: [CellThing]] = []

but - I can\'t :)

Und

3条回答
  •  臣服心动
    2020-11-29 08:20

    If you extend the Dictionary type you can use the already defined generic Key directly.

    extension Dictionary {
        // Key and Value are already defined by type dictionary, so it's available here
        func getSomething(key: Key) -> Value {
            return self[key]
        }
    }
    

    This works because Dictionary already has generics Key and Value defined for it's own use.

提交回复
热议问题