I expose the following in a local package in a Xcode project:
extension TaskSchedule {
public enum ValidationError: Error {
case endDateBeforeStartDate
case endDateInPast
}
public func validate(now: Date = .now) throws(ValidationError) {
// Validate endDate if present
if let endDate = endDate {
// Check: endDate > startDate
guard endDate > startDate else {
throw ValidationError.endDateBeforeStartDate
}
// Check: endDate > now
guard endDate > now else {
throw ValidationError.endDateInPast
}
}
}
}
Then in the project I try to do the following:
import Models
@State private var validationError: TaskSchedule.ValidationError?
//....
struct MyView: View {
var body: some View {
VStack {
// ...
}
.onChange(of: taskSchedule, initial: true) { _, newValue in
do {
try newValue.validate()
validationError = nil
} catch {
validationError = error
}
}
}
}
and I get the following error when I do the validationError = error
:
Cannot assign value of type 'any Error' to type 'TaskSchedule.ValidationError'
Shouldn't the type be available in this context? Am I missing something or is it a bug in the compiler?
I'm on Xcode 16.4, Swift 6.1.2