Type 'Error' does not conform to protocol 'RawRepresentable'

11.3k views Asked by At

Changing my playground code to Swift 3, Xcode suggested changing

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.

5

There are 5 answers

2
Hamish On BEST ANSWER

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).

You could refer to the Swift Error protocol as Swift.Error in order to disambiguate:

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).

Therefore the easiest solution by far would be simply renaming your error type to something more descriptive.

0
Qbyte On

This error occurs because you are "overriding" the existing declaration ofError which is a protocol. So you have to choose another (probably more descriptive) name for your "Error" enum.

0
nahung89 On

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.

The solution is specifying RealmSwift.Error in the declaration.

// before
enum MyError: Error { ... }
// after
enum MyError: Swift.Error { ... }
0
Edison On

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.

enum Error : Swift.Error {
    typealias RawValue = NSError

    case failedToAddInput
    case failedToAddOutput
    case failedToSetVideoOrientation
}
0
Marc Renaud On

I was getting this error because I forgot to put import Foundation at the top of my file. Just sharing in case it helps someone else.