How do I use reserved keyword `Type` in Swift?

I know, It doesn’t make sense. but I have no choice.
There are 2 problems

Code Block
// problem1
let type = ReservedKeyword.`Type`(rawValue: 1)
public class ReservedKeyword: Codable {
public enum `Type`: Int, Codable {
case zero = 0
case one = 1
case two = 2
case three = 3
}
public var type: ReservedKeyword.`Type`
public init(type: ReservedKeyword.`Type`) {
self.type = type
}
private enum CodingKeys: String, CodingKey {
case type
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// problem2
self.type = try container.decode(ReservedKeyword.`Type`.self, forKey: .type)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.type, forKey: .type)
}
}

An interesting example.

My best recommendation as for now is that avoid using the identifier Type and send a bug report to bugs.swift.org .

I have no choice.

If you mean you cannot change the identifier Type, typealias might be a workaround for problem 1.
Code Block
typealias Type = ReservedKeyword.`Type`
let type = Type(rawValue: 1)


For problem 2, you can use non-prefixed type name:
Code Block
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// problem2
self.type = try container.decode(`Type`.self, forKey: .type)
}


I'm not sure such workarounds would work in all future versions of Swift compilers. Better send the bug report soon.
@OOPer.
Thanks for help. It works.
How do I use reserved keyword `Type` in Swift?
 
 
Q