Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 767 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For-in loop requires '[UserVehicles]?' to conform to 'Sequence'; did you mean to unwrap optional? Swift

#1
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][1]][1]

Do you know how to fix it?


[1]:
Reply

#2
You can also use for_each loop for this, for eg like this:



vehicleList?._embedded.userVehicles.for_each(x in /*Do your thing here*/)
Reply

#3
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
}

Reply

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

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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through