【问题标题】:when implementing ErrorProtocol, keep getting error 1) use of undeclared type ErrorProtocol 2) type does not conform to protocol RawRepresentable实现 ErrorProtocol 时,不断出现错误 1) 使用未声明的类型 ErrorProtocol 2) 类型不符合协议 RawRepresentable
【发布时间】:2017-01-22 23:09:47
【问题描述】:

我正在按照 swift 编程语言 3.0 手册学习 swift。在错误处理一章中,当你想要声明一个错误类型时,你需要实现ErrorProtocol 协议。如下图:

enum VendingMachineError: ErrorProtocol{
    case invalidSelection
    case insufficientFuns(coinsNeeded: Int)
    case outOfStock
}

但在 xcode 游乐场,我不断收到错误消息

1) 使用未声明类型的ErrorProtocol

2) 类型 VendingMachineError 不符合协议 RawRepresentable

为什么会这样?我只是在处理书中的代码。

【问题讨论】:

  • enum VendingMachineError: Error {

标签: ios swift exception error-handling exception-handling


【解决方案1】:

不是ErrorProtocol,是ErrorType,你在创建错误枚举时声明

【讨论】:

  • OP 它没有使用 Swift 2,他使用的是 Swift 3 应该是Error
  • 非常感谢大家的帮助!令人费解的是,在 swift 编程语言 3.0 书中,它说我们应该使用 ErrorProtocol,即使我们假设在 swift 3 中使用 Error。再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多