Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum conformance to RawRepresentable

Tags:

swift

In the Swift book, the example for enums works fine

enum CompassPoint: String {
    case north, south, east, west
}

var northCom = CompassPoint.north
print (northCom)

However I want to use a failable initializer so made an example of this

enum WeekDay: String {
    case monday, tuesday, wednesday, thursday, friday
    init?(rawValue: Int){
        switch rawValue {
        case 0 : self = .monday
        case 1 : self = .tuesday
        case 2 : self = .wednesday
        case 3 : self = .thursday
        case 4 : self = .friday
        default : return nil
        }
    }
}

And get an error that Weekday does not conform to RawRepresentable - although I feel that conformance should be synthesized by the compiler so do not understand why this does not compile.

What I've done Created a similar working example (to see what the issue is), and still I want to conform to RawRepresentable using an enum with a failable initializer. I cannot find an example of this in the Swift book, in Stack Overflow questions or on the wider internet.

What I've provided Given a full example above, with the error as well as a working example of the behaviour I'm expecting.

What is not helpful Referencing the Swift book through a link or a comment is not helpful, as I've taken an example from there. I want to build on that example with a failable initializer. It is also not helpful to NOT use a enum, or a failable initializer. The question is about using a failable initalizer with a enum, and conforming to RawRepresentable. This is not homework, but those are the constraints of the question for my own learning and I'm interested in the outcome.

The question How can I use a failable initializer, with an enum as in the non-working example?

like image 556
WishIHadThreeGuns Avatar asked Apr 03 '19 07:04

WishIHadThreeGuns


1 Answers

Apparently your definition of init?(rawValue: Int) prevents the compiler from inferring the RawValue type automatically. Adding a type alias helps:

enum WeekDay: String {
    typealias RawValue = String

    case monday, tuesday, wednesday, thursday, friday

    init?(rawValue: Int){
        switch rawValue {
        case 0 : self = .monday
        case 1 : self = .tuesday
        case 2 : self = .wednesday
        case 3 : self = .thursday
        case 4 : self = .friday
        default : return nil
        }
    }
}

Alternatively define your custom init function with a different parameter name:

enum WeekDay: String {

    case monday, tuesday, wednesday, thursday, friday

    init?(rawInt: Int){
        switch rawInt {
        case 0 : self = .monday
        case 1 : self = .tuesday
        case 2 : self = .wednesday
        case 3 : self = .thursday
        case 4 : self = .friday
        default : return nil
        }
    }
}
like image 136
Martin R Avatar answered Nov 26 '22 10:11

Martin R