Swift “如何修复”;从X类型的抛出函数到Y类型的非抛出函数的转换无效;试抓
我得到了错误:从类型为“()throws->()”的抛出函数到非抛出函数类型“(或)->Void”的转换无效。 在Swift “如何修复”;从X类型的抛出函数到Y类型的非抛出函数的转换无效;试抓,swift,try-catch,Swift,Try Catch,我得到了错误:从类型为“()throws->()”的抛出函数到非抛出函数类型“(或)->Void”的转换无效。 在do{之后的行上,weatherApi.weather(带:weatherEndpoint){(或)in class WeatherTableViewController: UITableViewController { var cellViewModels = [WeatherCellViewModel]() override func viewDidLoad(
do{
之后的行上,weatherApi.weather(带:weatherEndpoint){(或)in
class WeatherTableViewController: UITableViewController {
var cellViewModels = [WeatherCellViewModel]()
override func viewDidLoad() {
super.viewDidLoad()
let weatherApi = WeatherAPIClient()
let weatherEndpoint = WeatherEndpoint.fiveDayForecast(city: "Atlanta", country: "us")
do {
weatherApi.weather(with: weatherEndpoint) { (either) in
switch either {
case .value(let Weather):
print("Made it")
print(Weather)
let data = try Weather.map {
$0.weather.map {
WeatherCellViewModel(description: $0.description)
}
}
print("data")
print(data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .error(let error):
print(error)
}
}
} catch {
print("weather endpoint error")
}
}
在最初获得相同错误并查看类似问题的答案后,我添加了do-try-catch,但它没有解决问题。try块是导致错误的关键部分,我正在解析数据以进行描述。
do-catch
当前在具有try
状态的闭包之外我不在里面
您需要将
do catch
移动到闭包中。错误是因为weather
希望闭包不会抛出,但它会继续抛出。do catch当前位于包含try
语句的闭包之外
您需要将
do catch
移动到闭包中。错误是因为weather
希望闭包不会抛出,但它会继续抛出。您希望从闭包中抛出,这是不可能的。因此,您需要将do catch
放置到闭包中
试着这样做:
do {
let data = try Weather.map {
$0.weather.map {
WeatherCellViewModel(description: $0.description)
}
}
} catch let error as NSError {
print(error)
}
您希望从闭包中抛出,这是不可能的。因此您需要将
do catch
放入闭包中
试着这样做:
do {
let data = try Weather.map {
$0.weather.map {
WeatherCellViewModel(description: $0.description)
}
}
} catch let error as NSError {
print(error)
}