ExpressibleByCustomTypeLiteral

问题描述

为什么没有这样的协议?
当然,只有当CustomType本身符合某些ExpressibleBySomeLiteral协议时,它才有意义。

struct First: ExpressibleByDictionaryLiteral {
    let data: [String: Int]
    
    init(dictionaryLiteral elements: (String,Int)...) {
        data = .init(uniqueKeysWithValues: elements)
    }
}

let f: First = ["one": 1,"two": 2,"three": 3]

struct Second: ExpressibleByArrayLiteral {
    let firsts: [First]
    
    init(arrayLiteral elements: First...) {
        firsts = elements
    }
}

let s: Second = [["one": 1,"three": 3],["four": 4,"five": 5],["six": 6,"seven": 7,"eight": 8,"nine": 9]]

上面的所有代码都是绝对有效的,可以编译并完美运行。
但是,当我们尝试以更简单的相同方式进行操作时,事实证明这是不可能的:

struct SimplifiedSecond: ExpressibleCustomTypeLiteral {
    let first: First

    init(customTypeLiteral element: First) {
        first = element
    }
}

let s: SimplifiedSecond = ["one": 1,"three": 3] 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)