简体   繁体   English

For-in 循环需要“[UserVehicles]?” 符合“顺序”; 你的意思是解开可选的? 迅速

[英]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.

相关问题 For-in 循环需要“JSON?” 符合“顺序”; 你的意思是解开可选的吗? - For-in loop requires 'JSON?' to conform to 'Sequence'; did you mean to unwrap optional? “for-in 循环需要 '[String]?' 符合“顺序”; 你的意思是解开可选的包装吗?” 但我不认为我在使用选项 - “For-in loop requires '[String]?' to conform to 'Sequence'; did you mean to unwrap optional?” but I don't think I am using optionals 在Swift中,如果您解开“ Implicitly Unwrapped Optional”,这是双重解开吗? - In Swift if you unwrap a “Implicitly Unwrapped Optional” is this a double unwrap? Swift可选值在while循环条件下解包 - Swift optional value Unwrap in while loop condition Swift问题:可选类型'[Int:Int]的值?' 不展开 你是说用'!' 要么 '?'? - Issue with Swift: value of optional type '[Int : Int]?' not unwrapped; did you mean to use '!' or '?'? 无法在swift中打开可选项 - can't unwrap optional in swift 无法在Swift中解开Optional - Can't unwrap Optional in Swift 可选类型“浮点型”的值 不展开 你是说用'!' 要么 '?'? - Value of optional type 'Float?' not unwrapped; did you mean to use '!' or '?'? 未包装的可选类型“ AnyObject?”的值不是您要使用的! 要么? - Value of optional Type “AnyObject?” not unwrapped did you mean to use ! or? 可选类型CGFloat的值? 没有解包,您是说要使用! 要么? - Value of optional type CGFloat? not unwrapped, did you mean to use ! or?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM