Подскажите, как можно достать значение из функции с Try/Catch
struct City {
let city: String
}
struct Ticket {
let price: Int
var count: Int
let flightTicket: City
}
enum SellError: Error {
case noFlight
case sold
case needMoreMoney(coins: Int)
}
class AirportCashbox2 {
var flightTickets = ["NYC": Ticket(price: 800, count:1, flightTicket: City(city: "NYC"))]
var money = 0
func sell2(_ city: String) throws -> City {
guard var ticket = flightTickets[city] else {
throw SellError.noFlight
}
guard ticket.count > 0 else {
throw SellError.sold
}
guard money >= ticket.price else {
let needMoney = money - ticket.price
throw SellError.needMoreMoney(coins: ticket.price - needMoney)
}
money -= ticket.price
ticket.count -= 1
flightTickets[city] = ticket
print("The ticket for the flight to \(city) was bought. \nCurrent balance: $\(money).\n")
return ticket.flightTicket
}
}
let cashbox2 = AirportCashbox2()
cashbox2.money = 530
do {
try cashbox2.sell2("NYC")
} catch SellError.noFlight {
print("There is no such flight.\n")
} catch SellError.sold{
print("All tickets are sold\n")
} catch SellError.needMoreMoney(let coins){ // (let coins) - предупреждение неиспользуемая переменная, без неё - ошибка
print("You need to add $\(-needMoney)\n"). // тут ошибка Use of unresolved identifier 'needMoney'
}
Как получить значение needMoney?