简体   繁体   English

类型'错误'不符合协议'RawRepresentable'

[英]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 . 发生此错误是因为您“覆盖”作为protocolError的现有声明。 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.ErrorRealmSwift.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM