Ios 无法分配给属性:“项”是“let”常量(Swift 4)
我正在尝试在用于处理调用的函数中创建一个循环,但遇到无法分配给属性的错误:“item”是一个“let”常量Ios 无法分配给属性:“项”是“let”常量(Swift 4),ios,swift,swift4,Ios,Swift,Swift4,我正在尝试在用于处理调用的函数中创建一个循环,但遇到无法分配给属性的错误:“item”是一个“let”常量 func didReceiveResponse(response:NearbyCars?, error : Error?) -> Void { dount += 1 if let error = error { let alertController = UIAlertController(title: "Error", me
func didReceiveResponse(response:NearbyCars?, error : Error?) -> Void {
dount += 1
if let error = error {
let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let actionDismiss = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil)
let actionRetry = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.loadPlaces(true)
}
})
alertController.addAction(actionRetry)
alertController.addAction(actionDismiss)
DispatchQueue.main.async {
self.present(viewController: alertController)
}
}
if let response = response {
self.response = response
if response.status == "OK" {
if let carsDownloaded = response.cars {
var number = numberCars / (categories?.count)!
let quotient = numberCars / (categories?.count)!
let remainder = numberCars % (categories?.count)!
for item in (categories?.enumerated())! {
item.element?.availability = quotient + (item.offset < remainder ? 1 : 0)
} // HERE THE ERROR
cars.append(contentsOf: carsDownloaded.prefix(number))
if dount == numberCars { return }
sortedArray = cars.sorted {
distance(from: currentLocation!, to: $0.location!) < distance(from: currentLocation!, to: $1.location!)
}
}
self.tableView?.reloadData()
} else {
let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.loadPlaces(true)
}
}))
self.present(viewController: alert)
}
isLoading = false
} else {
print("response is nil")
}
}
在该行,item.element?.availability=商+item.offset<余数?1:0,而我正在尝试创建一个分配其余数字/类别?的循环。计数!到现在起的类别var number=numberCars/categories?计数!只有在没有休息的时候才工作。如何修复它?您可以在一个范围内进行循环,然后通过索引访问类别以获得可变对象:
for i in 0..<(categories?.count)! {
var item = categories[i]
item.element?.availability = quotient + (item.offset < remainder ? 1 : 0)
}
这可能是重复的没有帮助@4kman这个技巧在swift4中不再有效。@dasblinkenlight虽然这是真的,但问题仍然是重复的。
guard let categories = categories else {
//Handle a nil categories array here
return
}