【发布时间】:2020-04-20 17:39:49
【问题描述】:
SwiftUI 的 @State 属性包装器有什么魔力,这意味着我可以做到这一点?:
struct A {
@State var s: String
}
let a = A(s: "string") //uses a synthesised init for `A` which allows me to init A with the underlying type of `A.s` - a `string`
而如果我自己滚动@propertyWrapper,我不能?
@propertyWrapper
struct Prop<Value> {
var value: Value
var wrappedValue: Value {
get { value }
set { value = newValue }
}
}
struct B {
@Prop var s: String
}
let b = B(s: "string") // Compiler error: `Cannot convert value of type 'String' to expected argument type 'Prop<String>'`
let b = B(s: Prop(value: "string")) // Works, but is ugly
【问题讨论】: