我对swift和他们的类型系统很新。我想创建一个字典,该字典可以包含String的键和符合QueryItem协议的项数组的值。
typealias ResultsItems = Array: [QueryItem] //= [Location(), Item()]
// ^^ getting Consecutive declarations on a line must be separated by ';'
typealias ResultDictionary = Dictionary<String, ResultsItems>
我不确定这里的类型是否正确。基本上我想要一本可以是这样的字典:
ResultDictionary:
- dataItems
- Item
- Item
- dataLocations
- Location
- Location
我如何为这样的回调声明这个?
class func query(term: String, completionHandler: (queryResults: Dictionary) -> ()) {
答案 0 :(得分:1)
类型应该是:
// [QueryItem] is shorthand for Array<QueryItem>
typealias ResultsItems = [QueryItem]
typealias ResultDictionary = Dictionary<String, ResultsItems>
作为改进,我建议使用[String : ResultsItems]
代替Dictionary<String, ResultsItems>
然后回调应该是
class func query(term: String, completionHandler: (queryResults: [String : ResultsItems]) -> ()) {
或者在这种情况下:
class func query(term: String, completionHandler: (queryResults: ResultDictionary) -> ()) {