Playground execution failed: /var/folders/8b/b2dxlhsd1s74pjswbjrr_7t40000gn/T/./lldb/377/playground449.swift:5:19: error: use of undeclared type 'Error' enum EnrollError: Error { ^~~~~ /var/folders/8b/b2dxlhsd1s74pjswbjrr_7t40000gn/T/./lldb/377/playground449.swift:5:6: error: type 'EnrollError' does not conform to protocol 'RawRepresentable' enum EnrollError: Error { ^ Swift.RawRepresentable:9:13: note: protocol requires nested type 'RawValue' typealias RawValue ^ /var/folders/8b/b2dxlhsd1s74pjswbjrr_7t40000gn/T/./lldb/377/playground449.swift:16:31: error: thrown expression type 'EnrollError' does not conform to 'ErrorType' throw EnrollError.notAvailable ~~~~~~~~~~~~^~~~~~~~~~~~ /var/folders/8b/b2dxlhsd1s74pjswbjrr_7t40000gn/T/./lldb/377/playground449.swift:19:31: error: thrown expression type 'EnrollError' does not conform to 'ErrorType' throw EnrollError.notQualified ~~~~~~~~~~~~^~~~~~~~~~~~ /var/folders/8b/b2dxlhsd1s74pjswbjrr_7t40000gn/T/./lldb/377/playground449.swift:22:31: error: thrown expression type 'EnrollError' does not conform to 'ErrorType' throw EnrollError.alreadyEnrolled
时间: 2023-08-28 16:26:20 浏览: 147
抱歉,可能是由于Swift版本的问题导致代码无法运行。以下是更新后的代码,希望能够解决问题:
```
enum EnrollError: Error {
case notAvailable
case notQualified
case alreadyEnrolled
}
class VendingCourse {
var availableSlots = 3
var isQualified = false
var isEnrolled = false
func enroll() throws {
guard availableSlots > 0 else {
throw EnrollError.notAvailable
}
guard isQualified else {
throw EnrollError.notQualified
}
guard !isEnrolled else {
throw EnrollError.alreadyEnrolled
}
availableSlots -= 1
isEnrolled = true
print("Enrollment successful!")
}
}
let course = VendingCourse()
course.isQualified = true
do {
try course.enroll()
} catch EnrollError.notAvailable {
print("No available slots.")
} catch EnrollError.notQualified {
print("Not qualified to enroll.")
} catch EnrollError.alreadyEnrolled {
print("Already enrolled.")
} catch {
print("An unknown error occurred.")
}
```
如果您还遇到问题,可以告诉我具体的错误信息,我会尽力帮助您解决问题。
阅读全文