对类型为“DecodedArray<T>"的无效关联类型“Iterator"的引用 [英] Reference to invalid associated type 'Iterator' of type 'DecodedArray<T>'
本文介绍了对类型为“DecodedArray<T>"的无效关联类型“Iterator"的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试借助以下教程进行解析
I am trying to parse with the help of below tutorial
https://swiftsenpai.com/swift/decode-dynamic-keys-json/
它给了我错误:对类型为DecodedArray"的无效关联类型迭代器"的引用**
It gives me error : Reference to invalid associated type 'Iterator' of type 'DecodedArray' **
subscript(index: Index) -> Iterator.Element {
get { return array[index] }
}
**
import UIKit
struct Student: Decodable {
let firstName: String?
let lastName: String?
// 1
// Define student ID
let studentId: String
// 2
// Define coding key for decoding use
enum CodingKeys: CodingKey {
case firstName
case lastName
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// 3
// Decode firstName & lastName
firstName = try container.decode(String.self, forKey: CodingKeys.firstName)
lastName = try container.decode(String.self, forKey: CodingKeys.lastName)
// 4
// Extract studentId from coding path
studentId = container.codingPath.first!.stringValue
}
}
struct DecodedArray<T: Decodable>: Decodable {
// ***
typealias DecodedArrayType = [T]
private var array: DecodedArrayType
// Define DynamicCodingKeys type needed for creating decoding container from JSONDecoder
private struct DynamicCodingKeys: CodingKey {
// Use for string-keyed dictionary
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
// Use for integer-keyed dictionary
var intValue: Int?
init?(intValue: Int) {
// We are not using this, thus just return nil
return nil
}
}
init(from decoder: Decoder) throws {
// Create decoding container using DynamicCodingKeys
// The container will contain all the JSON first level key
let container = try decoder.container(keyedBy: DynamicCodingKeys.self)
var tempArray = DecodedArrayType()
// Loop through each keys in container
for key in container.allKeys {
// ***
// Decode T using key & keep decoded T object in tempArray
let decodedObject = try container.decode(T.self, forKey: DynamicCodingKeys(stringValue: key.stringValue)!)
tempArray.append(decodedObject)
}
// Finish decoding all T objects. Thus assign tempArray to array.
array = tempArray
}
}
struct Food: Decodable {
let name: String
let category: String
enum CodingKeys: CodingKey {
case name
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Decode name
name = try container.decode(String.self, forKey: CodingKeys.name)
// Extract category from coding path
category = container.codingPath.first!.stringValue
}
}
extension DecodedArray: Collection {
// Required nested types, that tell Swift what our collection contains
typealias Index = DecodedArrayType.Index
typealias Element = DecodedArrayType.Element
// The upper and lower bounds of the collection, used in iterations
var startIndex: Index { return array.startIndex }
var endIndex: Index { return array.endIndex }
// Required subscript, based on a dictionary index
subscript(index: Index) -> Iterator.Element {
get { return array[index] }
}
//Reference to invalid associated type 'Iterator' of type 'DecodedArray<T>'
// Method that returns the next index when iterating
func index(after i: Index) -> Index {
return array.index(after: i)
}
}
推荐答案
这篇文章很晚了,我自己也遇到了这个问题.要解决这个问题,只需替换
Very late to this post, just ran into this problem myself. To resolve this simply replace
extension DecodedArray: Collection {
...
subscript(index: Index) -> Iterator.Element {
get { return array[index] }
}
}
与
subscript(position: DecodedArrayType.Index) -> DecodedArrayType.Element {
get { return array[position]}
}
这篇关于对类型为“DecodedArray<T>"的无效关联类型“Iterator"的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文