[英]Type 'Error' does not conform to protocol 'RawRepresentable'
Changing my playground code to Swift 3, Xcode suggested changing 将我的游乐场代码更改为Swift 3,Xcode建议更改
enum Error: ErrorType {
case NotFound
}
to 至
enum Error: Error {
case NotFound
}
but now I get the title error and I don't know how to get the enum to conform to that protocol. 但现在我得到标题错误,我不知道如何使枚举符合该协议。
The problem is that you've named your error type Error
– which conflicts with the standard library Error
protocol (therefore Swift thinks you've got a circular reference). 问题是你已经将错误类型命名为
Error
- 这与标准库Error
协议冲突(因此Swift认为你有一个循环引用)。
You could refer to the Swift Error
protocol as Swift.Error
in order to disambiguate: 您可以将Swift
Error
协议称为Swift.Error
以消除歧义:
enum Error : Swift.Error {
case NotFound
}
But this will mean that any future references to Error
in your module will refer to your Error
type, not the Swift Error
protocol (you'll have to disambiguate again). 但这意味着模块中将来对
Error
任何引用都将引用您的 Error
类型,而不是Swift Error
协议(您必须再次消除歧义)。
Therefore the easiest solution by far would be simply renaming your error type to something more descriptive. 因此,到目前为止最简单的解决方案是将错误类型重命名为更具描述性的内容。
This error occurs because you are "overriding" the existing declaration of Error
which is a protocol
. 发生此错误是因为您“覆盖”作为
protocol
的Error
的现有声明。 So you have to choose another (probably more descriptive) name for your "Error" enum
. 因此,您必须为“错误”
enum
选择另一个(可能更具描述性)名称。
I got this problem too, although i declared my enum with specific name. 我也遇到了这个问题,虽然我用特定的名字声明了我的枚举。
The reason is that I'm using Realm and it has Error
class, which makes the confusing between Swift.Error
and RealmSwift.Error
. 原因是我使用Realm并且它有
Error
类,这使得Swift.Error
和RealmSwift.Error
之间的混淆。
The solution is specifying RealmSwift.Error
in the declaration. 解决方案是在声明中指定
RealmSwift.Error
。
// before
enum MyError: Error { ... }
// after
enum MyError: Swift.Error { ... }
I tried this block in an AVCapture session and it works in Swift 3 + iOS 10. Using an NSError as a RawValue might address what Hamish was referring to above regarding future references to Error. 我在AVCapture会话中尝试了这个块,它在Swift 3 + iOS 10中工作。使用NSError作为RawValue可能会解决Hamish上面提到的有关Error的未来引用的问题。
enum Error : Swift.Error {
typealias RawValue = NSError
case failedToAddInput
case failedToAddOutput
case failedToSetVideoOrientation
}
I was getting this error because I forgot to put import Foundation
at the top of my file. 我收到此错误是因为我忘了将
import Foundation
放在我的文件顶部。 Just sharing in case it helps someone else. 只是分享它以帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.