[英]For-in loop requires '[UserVehicles]?' to conform to 'Sequence'; did you mean to unwrap optional? Swift
I have a data model which I made for API returns, it is something like this:我有一个用于 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
}
I have used callback function to pass it to my ViewController, now I want to get check in the useVehiclers list, how many vehicles hasBasicInsurance.我已经使用回调函数将它传递给我的 ViewController,现在我想检查 useVehiclers 列表,有多少辆车有基本保险。 basically,
vehicleList?._embedded.userVehicles[i] = true
基本上,
vehicleList?._embedded.userVehicles[i] = true
this is my function code to use the vehicle data in ViewController:这是我在 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)")
}
}
}
}
Do you know how to fix it?你知道如何解决吗?
您需要为 optional 提供默认值作为一种好习惯,而不是强制展开
for i in vehicleList?._embedded.userVehicles ?? [] { }
It's not clear from your code, but it looks like vehicleList
is optional.从您的代码中不清楚,但看起来
vehicleList
是可选的。 It probably should not be (see Leo Dabus's comments).它可能不应该是(见 Leo Dabus 的评论)。 It is rare that it makes sense to have an optional array.
很少有一个可选数组有意义。 That suggests there's some difference between an empty array and a missing array.
这表明空数组和缺失数组之间存在一些差异。 If there is, then that's fine, but in most cases you should just use a non-optional array and make it empty.
如果有,那很好,但在大多数情况下,您应该只使用非可选数组并将其设为空。
Whether you fix that or not, the solution to this particular problem is to just use a non-optional value, and you have one: data
.不管你是否修复了这个问题,解决这个特定问题的方法是只使用一个非可选值,你有一个:
data
。 So change the loop to:因此,将循环更改为:
for i in data._embedded.userVehicles { ... }
From your updated question, you note "I want to get check in the useVehiclers list, how many vehicles hasBasicInsurance."从您更新的问题中,您注意到“我想检查 useVehiclers 列表,有多少辆车有基本保险。” It seems you want to put that value in
i
.看来您想将该值放入
i
。 If so, that would be:如果是这样,那就是:
func useData(data: VehicleData) {
vehicleList = data
i = data._embedded.userVehicles
.filter(\.hasBasicInsurance)
.count
}
您也可以为此使用 for_each 循环,例如像这样:
vehicleList?._embedded.userVehicles.for_each(x in /*Do your thing here*/)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.