![](/img/trans.png)
[英]Swift extension - Constrained extension must be declared on the unspecialized generic type 'Array'
[英]Swift: Constrained extension on Dictionary by Element
我想在Dictionary上创建一个扩展,它只影响类型为[String:AnyObject]的字典,这是从解析的JSON字典返回的数据类型。 这是我如何设置它:
typealias JSONDictionary = [String : AnyObject]
extension Dictionary where Element:JSONDictionary {
// Some extra methods that are only valid for this type of dictionary.
}
Xcode在Element
上生成错误,称它是未声明的类型。 但是,Dictionary定义的第一行是声明Element的typealias。 我在这做错了什么?
Element
是一个元组:
typealias Element = (Key, Value)
这与您尝试将其与(词典)进行比较的类型无法匹配。 你甚至不能说像where Element:(String, AnyObject)
因为元组不是那种子类型。 例如,考虑:
var x: (CustomStringConvertible, CustomStringConvertible) = (1,1)
var y: (Int, Int) = (1,1)
x = y // Cannot express tuple conversion '(Int, Int)' to ('CustomStringConvertible', 'CustomStringConvertible')
相比:
var x1:CustomStringConvertible = 1
var y1:Int = 1
x1 = y1 // No problem
我怀疑你得到“未声明类型”是因为Element
不再是未绑定的类型参数,它是一个绑定类型参数。 Dictionary
符合SequenceType
。 所以你不能对它进行参数化(至少不是一步;你必须通过另一层类型参数来追逐它以发现它“最终”未绑定)。 这似乎是一个错误的错误消息,但我怀疑它出现了“可能在这里使用的类型列表中未声明的类型”。 我认为值得打开雷达以获得更好的错误信息。
相反,我认为你的意思是:
extension Dictionary where Key: String, Value: AnyObject { }
编辑Swift 2:
这不再是合法的斯威夫特。 您只能基于协议进行约束。 等效代码是:
protocol JSONKey {
func toString() -> String
}
extension String: JSONKey {
func toString() -> String { return self }
}
extension Dictionary where Key: JSONKey, Value: AnyObject { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.