Error type does not match RawRepresentable protocol

Changing the code of my playground to Swift 3, Xcode suggested changing

enum Error: ErrorType { case NotFound } 

to

 enum Error: Error { case NotFound } 

but now I get a header error, and I don't know how to get enum to conform to this protocol.

+6
source share
4 answers

The problem is that you named your error type Error - which conflicts with the standard Error protocol (so Swift thinks you have a circular link).

You can refer to the Swift Error protocol as Swift.Error to disambiguate:

 enum Error : Swift.Error { case NotFound } 

But this will mean that any future links to Error in your module will refer to your Error type, and not to the Swift Error protocol (you will have to fix the problem again).

Therefore, the easiest solution would be to simply rename your type of error to something more descriptive.

+24
source

This error occurs because you "override" an existing Error declaration, which is protocol . Therefore, you need to choose a different (possibly more descriptive) name for your "Error" enum .

+2
source

I also have this problem, although I declared my listing with a specific name.

The reason is that I use Realm and it has the Error class, which causes embarrassment between Swift.Error and RealmSwift.Error .

The solution indicates RealmSwift.Error in the declaration.

 // before enum MyError: Error { ... } // after enum MyError: Swift.Error { ... } 
+1
source

I tried this block in an AVCapture session, and it works in Swift 3 + iOS 10. Using NSError as RawValue may affect Hamish referencing future Error references above.

 enum Error : Swift.Error { typealias RawValue = NSError case failedToAddInput case failedToAddOutput case failedToSetVideoOrientation } 
0
source

All Articles