这是对 https://stackoverflow.com/a/47765972/8833459 的引用。 给出以下声明:
let fooPaths: [WritableKeyPathApplicator<Foo>] = [WritableKeyPathApplicator(Foo.bar), WritableKeyPathApplicator(Foo.baz)]
是否有一个 init 或其他可以做的事情,以便以下(或类似的东西(也可以工作?
let fooPaths: [WritableKeyPathApplicator<Foo>] = [Foo.bar, Foo.baz]
原话实在是打字太多了! 我的"首选"方法当前给出错误:
error: cannot convert value of type 'WritableKeyPath<Foo, String>' to expected element type 'WritableKeyPathApplicator<Foo>'
类型是不必要的:
let fooPaths = [WritableKeyPathApplicator(Foo.bar), WritableKeyPathApplicator(Foo.baz)]
第二Foo
是不必要的。
let fooPaths = [WritableKeyPathApplicator(Foo.bar), WritableKeyPathApplicator(.baz)]
如果您确实提供了类型,则也是第一个:
let fooPaths: [WritableKeyPathApplicator<Foo>] = [WritableKeyPathApplicator(.bar),
WritableKeyPathApplicator(.baz)]
如果主要关注点是键入,请添加本地类型别名:
typealias WKPA<T> = WritableKeyPathApplicator<T>
let fooPaths: [WKPA<Foo>] = [WKPA(.bar),
WKPA(.baz)]