Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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. basically, vehicleList?._embedded.userVehicles[i] = true

this is my function code to use the vehicle data in 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

Do you know how to fix it?

like image 415
Yudi Avatar asked Oct 17 '20 15:10

Yudi


2 Answers

You need to supply a default value for optional as a good practise instead of force unwrap

for i in vehicleList?._embedded.userVehicles ?? [] { }
like image 98
Sh_Khan Avatar answered Nov 12 '22 10:11

Sh_Khan


It's not clear from your code, but it looks like vehicleList is optional. It probably should not be (see Leo Dabus's comments). 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. 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." It seems you want to put that value in i. If so, that would be:

func useData(data: VehicleData) {
    vehicleList = data
    i = data._embedded.userVehicles
        .filter(\.hasBasicInsurance)
        .count
}
like image 1
Rob Napier Avatar answered Nov 12 '22 10:11

Rob Napier