假设:
struct Foo {
let bar: Bar
}
我使用一个方便的初始化程序:
let foo = Foo(bar: Bar())
但如果Bar
本身不是Codable
,或者出于其他某种原因我需要在Foo
上显式实现Codable
,那么方便的按成员初始化程序将不再存在:
struct Foo: Codable {
init(from decoder: Decoder) throws {
}
func encode(to encoder: Encoder) throws {
}
let bar: Bar
}
我得到了如下内容:
let foo = Foo(from: Bar())
调用中的参数标签不正确(应为'from:'而不是'bar:')
这里是否可能两全其美?