使用 if let 'someClass' as 进行类型转换? SomeClass 和绑定到 'someClass.'someProp' 不起作用

typecasting with if let 'someClass' as? SomeClass and binding to 'someClass.'someProp' does not work

我有以下结构

import SwiftUI

class TopClass: ObservableObject {
    @Published var someArr = [SomeClass(), SomeSubclass()]
}

class SomeClass: Identifiable {
    let id = UUID()
}

class SomeSubclass: SomeClass {
    var toggle = true
}

struct ContentView: View {
    @ObservedObject var topClass = TopClass()
    var body: some View {
        ForEach($topClass.someArr) {
            $value in
            if let someSubclass = $value as? SomeSubclass  {
                Toggle("Test", isOn: someSubclass.toggle)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

我不知道是否有可能以某种方式将 someSubclass.toggle 绑定到 isOnToggle。我将不胜感激我的解决方案或提示我可以研究的地方。

如果您真的hell-bent使用这种方法,那么试试这个示例代码:

struct ContentView: View {
    @ObservedObject var topClass = TopClass()
    
    var body: some View {
        ForEach($topClass.someArr) { $value in
            if let someSubclass = $value.wrappedValue as? SomeSubclass  {
                Toggle("test", isOn: Binding<Bool> (
                    get: { someSubclass.toggle },
                    set: {
                        topClass.objectWillChange.send()
                        someSubclass.toggle = [=10=]
                    })
                )
            }
        }
    }
}