There are examples on Swift book demonstrating associated values and raw values separately, is there a way to define enums with the two features together?
I have tri
I solved it like this:
enum Barcode {
case UPCA(Int, Int, Int)// = "Order 1"
case QRCode(String)// = "Order 2"
static func customRawValue(rawValue: String) -> Barcode? {
switch rawValue {
case "Order 1": return Barcode.UPCA(0, 0, 0)
case "Order 2": return Barcode.QRCode("")
default: return nil
}
}
var customRawValue : String {
switch self {
case .UPCA: return "Order 1"
case .QRCode: return "Order 2"
}
}
}
if let barcode = Barcode.customRawValue("Order 1") {
print("Barcode found with custom rawValue: \(barcode)")
print("Custom rawValue: \(barcode.customRawValue)")
}
It's somewhat hacky, but this solution worked great for my purpose!