- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想删除一个标记为 @ObjectBinding
的对象,例如为了清理一些 TextFields
。
我试图将对象引用设置为 nil,但没有成功。
import SwiftUI
import Combine
class A: BindableObject {
var didChange = PassthroughSubject<Void, Never>()
var text = "" { didSet { didChange.send() } }
}
class B {
var property = "asdf"
}
struct DetailView : View {
@ObjectBinding var myObject: A = A() //@ObjectBinding var myObject: A? = A() -> Gives an error.
@State var mySecondObject: B? = B()
var body: some View {
VStack {
TextField($myObject.text, placeholder: Text("Enter some text"))
Button(action: {
self.test()
}) {
Text("Clean up")
}
}
}
func test() {
//myObject = nil
mySecondObject = nil
}
}
如果我尝试使用可选的 @ObjectBinding
,我会收到错误
"Cannot convert the value of type 'ObjectBinding' to specified type 'A?'".
它只适用于 @State
。
问候
最佳答案
你可以这样做:
class A: BindableObject {
var didChange = PassthroughSubject<Void, Never>()
var form = FormData() { didSet { didChange.send() } }
struct FormData {
var firstname = ""
var lastname = ""
}
func cleanup() {
form = FormData()
}
}
struct DetailView : View {
@ObjectBinding var myObject: A = A()
var body: some View {
VStack {
TextField($myObject.form.firstname, placeholder: Text("Enter firstname"))
TextField($myObject.form.lastname, placeholder: Text("Enter lastname"))
Button(action: {
self.myObject.cleanup()
}) {
Text("Clean up")
}
}
}
}
关于swift - 如何删除 SwiftUI 中标有@ObjectBinding 的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57004311/
SwiftUI 具有 .animate() 的隐式动画,以及使用 .withAnimation() 的显式.但是,我不知道如何为图像更改设置动画: struct ImageViewWidget : V
我有一个 SwiftUI View ,它有一个 @ObjectBinding progression: Progressable 像这样: struct ProgressBarComponent :
当我们有 @Binding 属性包装器时,@EnvironmentObject 的目的是什么?我无法弄清楚为什么需要@EnvironmentObject。我查看了这些帖子,但它并不是我想要的 Envi
我想删除一个标记为 @ObjectBinding 的对象,例如为了清理一些 TextFields。 我试图将对象引用设置为 nil,但没有成功。 import SwiftUI import Combi
我正在尝试学习Combine,它对我来说是一个PITA。我从未学过 RX Swift,所以这对我来说是全新的。我确信我错过了一些简单的事情,但希望得到一些帮助。 我正在尝试从 API 获取一些 JSO
我正在尝试在 SwiftUI 中设置一个基本列表,但我遇到了一个对我来说意义不大的编译时错误。这是错误: Cannot convert value of type 'Text' to closure
我有一个简单的 bean,它有一些相互关联的属性。例如,这个 bean 有一个名为 discountRate 的属性和另一个名为 discountValue 的属性。 discountRate 是应用
在 SwiftUI 中,您可以将 Publisher 的实例直接用作 @ObjectBinding 属性,还是必须将其包装在实现 BindableObject 的类中? let subject = P
我正在测试 Combine 框架并使用 BindableObject 作为通知中心,以便在 SwiftUI ContentView 的多个 View 之间传递数据。 其中一个 View 是一个表。我单
我是一名优秀的程序员,十分优秀!