I'm trying to create an extension for my FieldIdentifiable protocol only where the enum that implements it has a RawValue of Int. The only problem is that the return FieldIdItem(rawValue: newValue)
line keeps showing this error:
'Self.FieldIdItem' cannot be constructed because it has no accessible initializers
Is this a Swift bug or am I missing something?
enum SignUpField: Int, FieldIdentifiable {
case Email = 0, Password, Username
typealias FieldIdItem = SignUpField
}
protocol FieldIdentifiable {
typealias FieldIdItem
func next() -> FieldIdItem?
func previous() -> FieldIdItem?
}
extension FieldIdentifiable where Self: RawRepresentable, Self.RawValue == Int {
func next() -> FieldIdItem? {
let newValue: Int = self.rawValue+1
return FieldIdItem(rawValue: newValue)
}
func previous() -> FieldIdItem? {
return FieldIdItem(rawValue: self.rawValue-1)
}
}