Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- enum SomeEnum: String, ExpressibleByStringLiteral, Encodable {
- case option1
- case option2
- case option3
- init(stringLiteral value: String) {
- switch value {
- case "option1": self = .option1
- case "option2": self = .option2
- case "option3": self = .option3
- default: fatalError("Case is not allowed")
- }
- }
- init(extendedGraphemeClusterLiteral value: String) {
- self.init(stringLiteral: value)
- }
- init(unicodeScalarLiteral value: String) {
- self.init(stringLiteral: value)
- }
- }
- protocol RestrictedEnum {}
- class Query1 {
- enum Query1Enum: SomeEnum {
- case option2 = "option2"
- case option3 = "option3"
- }
- init(_ options: Query1Enum...) {
- }
- }
- class Query2: Encodable {
- enum Query2Enum: SomeEnum, Encodable {
- case option2 = "option2"
- case option3 = "option3"
- func encode(to encoder: Encoder) throws {
- var c = encoder.singleValueContainer()
- try c.encode(self.rawValue)
- }
- }
- let options: [Query2Enum]
- init(_ options: Query2Enum...) {
- self.options = options
- }
- }
- let x = Query1(.option2)
- let y = Query2(.option2, .option3)
- let zData = "option2".data(using: .utf8)
- //let z = try! JSONDecoder().decode(SomeEnum.self, from: zData!)
- let z1 = try! JSONEncoder().encode(Query2(.option2))
- //print(z)
- print(String(data: z1, encoding: .utf8))
Add Comment
Please, Sign In to add comment