我有以下xcode游乐场代码来说明此问题:
import SwiftUI
struct S1 {
var list = ["a", "b"]
}
class C1 {
var s1 = State(initialValue: S1())
var s2 = S1()
}
var c1 = C1()
var list = c1.s1.wrappedValue.list
list.append("d")
var s1_modified = c1.s1.wrappedValue
s1_modified.list = list
s1_modified.list
// prints ["a","b","d"]
c1.s2 = s1_modified
c1.s2.list
// prints ["a","b","d"] as expected
c1.s1.wrappedValue = s1_modified
c1.s1.wrappedValue.list
// prints ["a","b"]
// expected ["a","b","d"]
我已经尝试了许多变化。我需要一个State实例在复杂的层次结构中传递各种视图对象,并且我不想在每个实例中创建一个单独的State(太混乱了)。将值手动分配给未由@State包装器包装的State实例真的难吗?我真的希望他们能开源此代码,以便我们能在几分钟内解决此类问题,而不是浪费大量时间猜测他们在做什么。