首页 > 解决方案 > 从原始值推断 Swift 初始化器

问题描述

我有以下 Swift 枚举,可确保仅使用纯 json 类型。

public enum JSONValue {
    case string(String)
    case integer(Int)
    case double(Double)
    case bool(Bool)

    public init(_ value: String) {
        self = .string(value)
    }

    public init(_ value: Int) {
        self = .integer(value)
    }

    public init(_ value: Double) {
        self = .double(value)
    }

    public init(_ value: Bool) {
        self = .bool(value)
    }
}

要初始化 JSON 值,必须这样做

let json = JSONValue.string("my value")

或者在字典的情况下

let params: [String: JSONValue] = [
    "my string": JSONValue.string("my value"),
    "my int": JSONValue.init(10)
]

有没有办法从原始值推断初始化程序以方便这样的使用:

let json: JSONValue = "my value"

let params: [String: JSONValue] = [
    "my string": "my value",
    "my int": 10
]

(题外话,但如果你想知道为什么我需要这个 JSONValue 枚举,这就是原因

标签: swift

解决方案


我认为您需要遵守以下协议:

  • ExpressibleByBooleanLiteral
  • ExpressibleByIntegerLiteral
  • ExpressibleByFloatLiteral
  • ExpressibleByStringLiteral

像这样

public enum JSONValue: ExpressibleByBooleanLiteral, ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByStringLiteral {
    public typealias BooleanLiteralType = Bool
    public typealias IntegerLiteralType = Int
    public typealias FloatLiteralType = Double
    public typealias StringLiteralType = String

    case string(String)
    case integer(Int)
    case double(Double)
    case bool(Bool)

    public init(stringLiteral value: String) {
        self = .string(value)
    }

    public init(integerLiteral value: Int) {
        self = .integer(value)
    }

    public init(floatLiteral value: Double) {
        self = .double(value)
    }

    public init(booleanLiteral value: Bool) {
        self = .bool(value)
    }
}

这将允许编译器执行一些魔术:

let jsonValue: JSONValue = "Hello World"

推荐阅读