gpt4 book ai didi

ios - For-in 循环要求 '[UserVehicles]?' 符合 'Sequence' ;你的意思是解开可选的吗? swift

转载 作者:行者123 更新时间:2023-12-03 15:27:15 24 4
gpt4 key购买 nike

我有一个用于 API 返回的数据模型,它是这样的:

struct VehicleData: Codable {

let _embedded: Embedded

}

struct Embedded: Codable {
let userVehicles: [UserVehicles]
}


struct UserVehicles: Codable {
let id: String
let images: [String]
let userId: String
let vehicle: Vehicle
let originalPrice: OriginalPrice
let hasBasicInsurance: Bool

}

我已经使用回调函数将它传递给我的 ViewController,现在我想查看 useVehicles 列表,有多少车辆有基本保险。基本上, vehicleList?._embedded.userVehicles[i] = true这是我在 ViewController 中使用车辆数据的功能代码:
    var vehicleManager = VehicleManager()
var vehicleList: VehicleData?
var i: Int = 0

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

vehicleManager.retrieveUserVehicle()
vehicleManager.onDataUpdate = { [weak self] (data: VehicleData) in
self?.useData(data: data)
}

tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView() //remove empty tableView cells
tableView.register(UINib(nibName: Constants.vehicleListCellNibName, bundle: nil), forCellReuseIdentifier: Constants.vehicleListToBeInsuredIdentifier)

}

func useData(data: VehicleData) {
vehicleList = data

// code below has issues....
for i in [vehicleList?._embedded.userVehicles] {

if let vechile = vehicleList?._embedded.userVehicles[i].hasBasicInsurance {
if vehicle == true {
i = i + 1
print(">>number of of insured vehidle: \(i)")
} else {
print(">>>number of of insured vehidle: \(i)")
}
}



}

}

enter image description here
你知道如何解决吗?

最佳答案

您需要为 optional 提供默认值作为一种好习惯,而不是强制展开

for i in vehicleList?._embedded.userVehicles ?? [] { }

关于ios - For-in 循环要求 '[UserVehicles]?' 符合 'Sequence' ;你的意思是解开可选的吗? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64404208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com