Swift委托变量在函数被调用时总是nil

vddsk6oq  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(134)

我已经创建了下面的委托,无论我尝试什么,当调用reconcile()函数时,selectedTransaction都返回为nil。我将介绍委托模式,因为我有不同的模式和数据需求等。

class MatchReconciliationViewState: ReconciliationDelegate {
    var selectedTransaction: Transaction?
    let statementLine: StatementLine
    
    init(selectedTransaction: Transaction? = nil, statementLine: StatementLine) {
        self.selectedTransaction = selectedTransaction
        self.statementLine = statementLine
    }
    
    func canReconcile() -> Bool {
        return nil != selectedTransaction
    }
    
    func reconcile() {
        print("reconcile: \(String(describing: selectedTransaction))")
        guard let selectedTransaction = selectedTransaction else { return }
        
        ReconciliationFunctions.match(statementLine: statementLine, toTransaction: selectedTransaction)
    }
}

子视图是一个简单的列表,它选择列表中的对象(这部分工作)。onChange中的这些值打印显示我实际上持有数据(onChange发生在子视图中):

.onChange(of: selectedTransaction) { newValue in
    print("onChange: \(String(describing: selectedTransaction))")
    reconciliationDelegate.selectedTransaction = newValue
    parentState.canReconcile = reconciliationDelegate.canReconcile()
    print("onChangeDelegate: \(String(describing: reconciliationDelegate.selectedTransaction))")
}

但是当我开始使用它时,selectedTransaction的值总是nil(从父视图上的按钮触发):

private func reconcileStatementLine() {
    print("delegate: \(String(describing: state.reconciliationDeleage))")
    state.reconciliationDeleage?.reconcile()
//        persistence.commit()
//        presentationMode.wrappedValue.dismiss()
}

协议:

public protocol ReconciliationDelegate: AnyObject {
    func canReconcile() -> Bool
    func reconcile()
}

浏览次数:

@StateObject var state: ReconciliationViewState

var body: some View {
    Button(action: reconcileStatementLine) {
        Text("Reconcile")
    }
}

private func reconcileStatementLine() {
    print("delegate: \(String(describing: state.reconciliationDeleage))")
    state.reconciliationDeleage?.reconcile()
//        persistence.commit()
//        presentationMode.wrappedValue.dismiss()
}

ReconciliationViewState保存这样的值(有@Published属性,但与问题无关):

class ReconciliationViewState: ObservableObject {
    var reconciliationDeleage: ReconciliationDelegate? = nil
}

我错过了什么,还是不明白?

oxcyiej7

oxcyiej71#

我仍然不太明白为什么swift / swift UI以这种方式工作,或者为什么这解决了这个问题,但这是我在chatGPT的帮助下得到的。
声明变量的方式似乎是什么做的伎俩:

//    var reconciliationDelegate: MatchReconciliationViewState
    var reconciliationDelegate: MatchReconciliationViewState {
            get { parentState.ReconciliationDelegate as! MatchReconciliationViewState }
        }
    
// [...]

.onAppear() {
            let delegate = MatchReconciliationViewState(statementLine: parentState.statementLine)
            parentState.ReconciliationDelegate = delegate
}
// instead of during init due to how the variable is now declared.

旧的init代码:

var reconciliationDelegate: MatchReconciliationViewState

init(/*[...]*/) {
// [...]
reconciliationDelegate = MatchReconciliationViewState(statementLine: parentState.statementLine)
parentState.ReconciliationDelegate = reconciliationDelegate
// [...]

非常奇怪的行为,因为我确实引用了示例,函数的正确实现正在执行,但委托的属性selectedTransaction在委托的函数执行期间会返回到nil。

相关问题