结构数组中结构的访问元素

Access element of the struct in array of structs

嗨,我有一个任务要实现 Fleet 协议,它有两个功能:

addNewCar - 向车队添加新的汽车对象。 - 参数汽车:汽车添加到舰队 - Returns:如果车队中已经存在具有相同 ID 的汽车,则为 false,否则为 true。

deleteCar - 从车队中删除具有指定 ID 的汽车。 - Returns:如果车队中存在具有相同 ID 的汽车,则为真,否则为假。

listCarsByModel - returns 10 个包含指定字符串的汽车模型。 如果有几辆相同型号的汽车,则以“品牌 - 汽车”的格式将品牌名称添加到汽车型号中, 否则 returns 只是“汽车”。

listCarsByBrand - returns10个品牌包含指定字符串的车型, 结果按品牌排序。

struct Car {
    let id: String; // unique identifier
    let model: String;
    let brand: String;
}

protocol Fleet {
    func addNewCar(car: Car) -> Bool

    func deleteCar(id: String) -> Bool

    func listCarsByModel(searchString: String) -> Set<String>

    func listCarsByBrand(searchString: String) -> [String]

}


class FleetImpl: Fleet {
    var cars: [Car] = []

    func addNewCar(car: Car) -> Bool {
        if let i = cars.firstIndex(where: { [=10=].id == car.id }) {
            print(i)
            return false
        } else {
            cars.append(car)
            print(car)
            return true
        }
    }

    func deleteCar(id: String) -> Bool {

        return true
    }

    func listCarsByModel(searchString: String) -> Set<String> {

    }

    func listCarsByBrand(searchString: String) -> [String] {

    }
}

我使用了方法 firstIndex(where:)。但是该函数将新车添加到具有现有 ID 的数组中。 (即两辆或多辆具有相同 ID 的汽车)

如何访问 Car 结构的 'id' 属性 以操作该数据? 请帮帮我

这是测试代码:

func test(fleet: Fleet) {
    assert(!fleet.deleteCar(id: "1"))
    assert(fleet.addNewCar(car: Car(id: "1", model: "1", brand: "Lex")))
    assert(!fleet.addNewCar(car: Car(id: "1", model: "any name because we check id only", brand: "any brand")))
    assert(fleet.deleteCar(id: "1"))
    assert(fleet.addNewCar(car: Car(id: "3", model: "Some Car3", brand: "Some Brand2")))
    assert(fleet.addNewCar(car: Car(id: "4", model: "Some Car1", brand: "Some Brand3")))

    var byModels: Set<String> = fleet.listCarsByModels(searchString: "Car")
    assert(byModels.count == 10)

byModels = fleet.listCarsByModels(searchString: "Some Car")
    assert(byModels.count == 4)
    assert(byModels.contains("Some Brand3 - Some Car1"))
    assert(byModels.contains("Some Car2"))
    assert(byModels.contains("Some Car3"))
    assert(!byModels.contains("Some Car1"))
    assert(byModels.contains("Some Brand1 - Some Car1"))

var byBrand: [String] = fleet.listCarsByBrand(searchString: "Brand")
    assert(byBrand.count == 10)

byBrand = fleet.listCarsByBrand(searchString: "Some Brand")
    assert(byBrand.count == 4)
    assert(byBrand[0] == "Some Car1")
    assert(byBrand[1] == "Some Car2" || byBrand[1] == "Some Car3")
    assert(byBrand[2] == "Some Car2" || byBrand[2] == "Some Car3")
    assert(byBrand[3] == "Some Car1")
}

test(fleet: FleetImpl())

在我们处理 unique 元素时,在这种情况下,集合而不是数组可能会更好。另请注意,我没有使用 firstIndex(where),而是使用 first(where),这将 return 汽车而不是索引。

import UIKit

struct Car: Hashable {
    let id: String
    let model: String
    let brand: String
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

protocol Fleet {
    func addNewCar(car: Car) -> Bool
    func deleteCar(id: String) -> Bool
    func listCarsByModel(searchString: String) -> [String]
    func listCarsByBrand(searchString: String) -> [String]
}

class FleetImpl: Fleet {
    var cars = Set<Car>()

    func addNewCar(car: Car) -> Bool {
        guard !cars.contains(where: { [=10=].id == car.id }) else { return false }
        
        cars.insert(car)
        return true
    }

    func deleteCar(id: String) -> Bool {
        guard let car = cars.first(where: { [=10=].id == id }) else { return false }
        
        cars.remove(car)
        return true
    }
    
    func listCarsByModel(searchString: String) -> [String] {
        let searchedCars = cars.filter { [=10=].model.lowercased().contains(searchString.lowercased()) }.sorted { [=10=].brand < .brand }
        var formattedModels = [String]()
        searchedCars.forEach { car in
            if let car = searchedCars.first(where: { [=10=].model == car.model && [=10=].id != car.id }) {
                let carName = [car.model, car.brand].joined(separator: " - ")
                formattedModels.append(carName)
            } else {
                formattedModels.append(car.model)
            }
        }
        return Array(formattedModels.prefix(10))
    }
    
    func listCarsByBrand(searchString: String) -> [String] {
        let searchedBrands = cars.filter { [=10=].brand.lowercased().contains(searchString.lowercased()) }.sorted { [=10=].brand < .brand }.map { [=10=].model }
        return Array(searchedBrands.prefix(10))
    }
    
}