在我看来,我定义了一些ObservedObjects。 ShoppingItems和Packages是NSManagedObject子类。
@ObservedObject var shoppingListData: ShoppingListData
@ObservedObject var shoppingItem: ShoppingItems
@ObservedObject var package: Packages
init(thisData: ShoppingListData, formShoppingItem: ShoppingItems, formPackage: Packages?) {
shoppingListData = thisData
shoppingItem = formShoppingItem
if let fp = formPackage {
package = fp
} else if let fp = formShoppingItem.minStorePackage {
package = fp
} else {
let newPackage = Packages(context: thisData.dataStack.moc)
newPackage.myShoppingItem = formShoppingItem
newPackage.myStore = thisData.dataStack.currentReceipt.myStore!
package = newPackage
}
}
我体内有一个选择器。
Picker(selection: $package, label: Text("Store Packages")) {
ForEach(shoppingItem.storePackages, id: \.uuidKey) { pkg in
Text(pkg.brandName).tag(pkg.uuidKey)
}
}
选择器将显示其他软件包,但不会将软件包变量从旧软件包更改为新选择的软件包。 SwiftUI是否有一些限制,无法在视图初始化后更改@ObservedObject指向哪个实例?