我正在使用Swift 4编程语言的一些新功能。出于某种原因,它在下面的代码中抱怨我返回self [key]值。
extension Dictionary {
subscript<T>(key :String, type :T) -> T? {
get {
return self[key] as? T // this line
}
}
}
MyPlayground.playground:1:5:注意:找到了这个候选人
subscript<T>(key :String, type :T) -> T? {
任何想法我做错了什么?
更新:
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