Декодируемый: фильтровать элементы в массиве, которые не могут быть декодированы

Как я могу убедиться, что я отфильтровываю только человека B ниже, поскольку человек B является единственным искаженным объектом?

В документации для currentIndex на UnkeyedDecodingContainer указано:

Текущий индекс декодирования контейнера (т. е. индекс следующего элемента для декодирования). Увеличивается после каждого успешного вызова декодирования.

Кажется, что если вы попытаетесь отфильтровать ошибку декодирования с помощью try?, она никогда не перейдет к следующему элементу в массиве.

Но как этого добиться? Обратите внимание, что структуру JSON нельзя изменить.

let json = """
{
    "collection" : [
        {
            "name" : "A",
            "surname" : "ob"
        },
        {
            "name" : "B"
        },
        {
            "name" : "C",
            "surname" : "ob"
        },
        {
            "name" : "D",
            "surname" : "ob"
        }
    ]
}
""".data(using: .utf8)!

struct Person: Decodable {
    let name: String
    let surname: String
}

struct Collection: Decodable {
    let items: [Person]

    private enum CodingKeys: String, CodingKey {
        case items = "collection"
    }

    public init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)

        var itemsArr: [Person] = []

        var itemValues = try values.nestedUnkeyedContainer(forKey: .items)
        if let count = itemValues.count {
            for val in (0..<count) {
                print("trying \(val) at index: \(itemValues.currentIndex)")
                if let element = try? itemValues.decode(Person.self) {
                    print("adding \(val)")
                    itemsArr.append(element)
                }
            }
        }

        items = itemsArr
    }
}

let decoder = JSONDecoder()
let collection = try decoder.decode(Collection.self, from: json)
print("collection: \(collection)")

Производит вывод:

trying 0 at index: 0
adding 0
trying 1 at index: 1
trying 2 at index: 1
trying 3 at index: 1
collection: Collection(items: [__lldb_expr_101.Person(name: "A", surname: "ob")])

person Remover    schedule 28.11.2017    source источник