I am trying to make a generic failable initializer with optional parameter for RawRepresentable, basically this https://www.natashatherobot.com/swift-failable-enums-with-optionals/
There were a couple of methods proposed one of which is this (EDIT: fixed let
in the second clause):
extension RawRepresentable {
init?(rawValue optionalRawValue: RawValue?) {
guard let rawValue = optionalRawValue, let value = Self(rawValue: rawValue) else { return nil }
self = value
}
}
from here https://gist.github.com/okla/e5dd8fbb4e604dabcdc3
I have no idea if it was ever working on Swift 2 but I can't compile it on Swift 3. I get:
Command failed due to signal: Segmentation fault: 11
Is there a way to make it work?
P.S. I am aware of other approaches from the article and its comments.
EDIT: Fixed broken copy/pasted code.