[英]Extending Optional<Dictionary<String, AnyObject>>
I would like to make an extension on Optional<Dictionary<String, AnyObject>>. 我想对Optional <Dictionary <String,AnyObject >>进行扩展。 How can I write this?
我该怎么写?
I was expecting it to be something like 我原以为它会像
extension Optional where
Wrapped : Dictionary,
Key : String,
Value : AnyObject { ... }
but it wants Dictionary to take the parameters directly, and even if this worked I expect it would complain that String was not a protocol. 但是它希望Dictionary直接采用参数,即使这样行之有效,我也希望它会抱怨String不是协议。 So I try
所以我尝试
extension Optional where Wrapped : Dictionary<String, AnyObject> { ... }
but here it complains that type 'Wrapped' constrained to non-protocol type 'Dictionary<String, AnyObject>'
但是在这里它抱怨
type 'Wrapped' constrained to non-protocol type 'Dictionary<String, AnyObject>'
use dot syntax to access the Key and Value of Wrapped. 使用点语法访问“包装的键和值”。
extension Optional where Wrapped: DictionaryLiteralConvertible, Wrapped.Key: StringLiteralConvertible, Wrapped.Value: AnyObject {
func doSomething() {
print("did it")
}
}
now you can call this: Optional(["asdf":123]).doSomething()
现在您可以称之为:
Optional(["asdf":123]).doSomething()
but not this: Optional([123:123]).doSomething()
但不是这个:
Optional([123:123]).doSomething()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.