Hello, I would like throw error when the format data is incorrect. But I don't know how to throw error outside the builde
r closure at init
import Foundation
struct DateFormatError: Error {}
struct Day {
let event: String
let date: Date
init(event: String, textDate: String) throws {
self.event = event
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd.MM.yyyy"
guard let date = dateFormatter.date(from: textDate) else { throw DateFormatError() }
self.date = Calendar.current.startOfDay(for: date)
}
}
struct Week {
let days: [Day]
init(@WeekBuilder _ builder: () -> [Day]) { // how to make that initialiser 'throws' ?
self.days = builder()
}
}
@resultBuilder
struct WeekBuilder {
static func buildBlock(_ components: (date: String, event: String)...) throws -> [Day] {
let array = try components.map { try Day(event: $0.date, textDate: $0.event) }
return array
}
}
And how to use that? Because now I have compiler error
// Using
struct WeekTest {
func test() {
do {
let week = try Week { // Error: Call can throw, but it is not marked with 'try' and the error is not handled
(date: "12.11.2022", event: "one")
(date: "13.11.2022", event: "two")
}
} catch { //'catch' block is unreachable because no errors are thrown in 'do' block
}
}
}
I did not find any mention of throws in the proposal