Swift:Dictionary by Dictionary的约束扩展

时间:2015-07-30 22:08:22

标签: ios swift

我想在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。我在这里做错了什么?

1 个答案:

答案 0 :(得分:9)

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:

这不再是合法的Swift。您只能基于协议进行约束。等效代码为:

protocol JSONKey {
    func toString() -> String
}
extension String: JSONKey {
    func toString() -> String { return self }
}

extension Dictionary where Key: JSONKey, Value: AnyObject { ... }