I have tried to boil this issue down to its simplest form with the following.
Xcode Version 6.1.1 (6A2008a)
An enum defined in MyEnum.swift
:
internal enum MyEnum: Int { case Zero = 0, One, Two } extension MyEnum { init?(string: String) { switch string.lowercaseString { case "zero": self = .Zero case "one": self = .One case "two": self = .Two default: return nil } } }
and code that initializes the enum in another file, MyClass.swift
:
internal class MyClass { let foo = MyEnum(rawValue: 0) // Error let fooStr = MyEnum(string: "zero") func testFunc() { let bar = MyEnum(rawValue: 1) // Error let barStr = MyEnum(string: "one") } }
Xcode gives me the following error when attempting to initialize MyEnum
with its raw-value initializer:
Cannot convert the expression's type '(rawValue: IntegerLiteralConvertible)' to type 'MyEnum?'
Per the Swift Language Guide:
If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called
rawValue
) and returns either an enumeration member ornil
.
The custom initializer for MyEnum
was defined in an extension to test whether the enum's raw-value initializer was being removed because of the following case from the Language Guide. However, it achieves the same error result.
Note that if you define a custom initializer for a value type, you will no longer have access to the default initializer (or the memberwise initializer, if it is a structure) for that type. [...]
If you want your custom value type to be initializable with the default initializer and memberwise initializer, and also with your own custom initializers, write your custom initializers in an extension rather than as part of the value type’s original implementation.
Moving the enum definition to MyClass.swift
resolves the error for bar
but not for foo
.
Removing the custom initializer resolves both errors.
One workaround is to include the following function in the enum definition and use it in place of the provided raw-value initializer. So it seems as if adding a custom initializer has a similar effect to marking the raw-value initializer private
.
init?(raw: Int) { self.init(rawValue: raw) }
Explicitly declaring protocol conformance to RawRepresentable
in MyClass.swift
resolves the inline error for bar
, but results in a linker error about duplicate symbols (because raw-value type enums implicitly conform to RawRepresentable
).
extension MyEnum: RawRepresentable {}
Can anyone provide a little more insight into what's going on here? Why isn't the raw-value initializer accessible?
This bug is solved in Xcode 7 and Swift 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With