I am using some new features of Swift 4 programming language. For some reason it is complaining in the below code where I am returning the self[key] value.
extension Dictionary {
subscript<T>(key :String, type :T) -> T? {
get {
return self[key] as? T // this line
}
}
}
MyPlayground.playground:1:5: note: found this candidate
subscript<T>(key :String, type :T) -> T? {
Any ideas what I am doing wrong?
UPDATE:
extension Dictionary {
subscript<T>(key :Key) -> T? {
return self[key] as? T
}
}
struct Pokemon {
var title :String
}
let pokemon = Pokemon(title: "Pikachu")
var dictionary = [String:Any]()
dictionary["foo"] = "hello world"
dictionary["pokemon"] = pokemon
let p = dictionary["pokemon"] // this calls the default subscript not mine