class 在委托方法中传递的对象在 Mac 催化剂中变为 <uninitialized>

class object passed in delegate method becomes <uninitialized> in Mac catalyst

在委托方法中传递的对象在实现协议的 class 中未初始化。但同一个对象在 class 中有有效地址,从那里调用委托。这只发生在 maccatalyst 中,相同的代码在 iOS 和 iPadOS

中完美运行

协议声明

 protocol ASPatientSearchDelegate: class {
    func moveToPaitentProfileScreen(patient: ASCardEntity)
}

委托变量声明

    weak var delegate: ASPatientSearchDelegate? = nil

委托调用

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let patient = self.searchResults[indexPath.row]

                self.delegate?.moveToPaitentProfileScreen(patient: patient)
}

方法实现

extension ASAppointmentViewController: ASPatientSearchDelegate {
        func moveToPaitentProfileScreen(patient: ASCardEntity) {
    
            guard let json = patient.details as? JSON else { return }
    }
}

在 ASAPpointmentViewController 中分配委托

   @IBAction func searchButtonAction(_ sender: UIButton) {

        let searchViewController = ASPatientSearchViewController(nibName: ASPatientSearchViewController.className, bundle: nil)

        searchViewController.popoverPresentationController?.delegate = self
        searchViewController.modelController.delegate = self
}

Class 二手

class ASCardEntity: NSObject {
    var details: Any?
}

这是实例范围问题。我已将 ASPatientSearchViewController 实例变量声明为 class 范围

实例变量声明

class ASAppointmentViewController: ASBaseViewController {
    var searchViewController: ASPatientSearchViewController?
}

我更改了以下代码

@IBAction func searchButtonAction(_ sender: UIButton) {

        let searchViewController = ASPatientSearchViewController(nibName: ASPatientSearchViewController.className, bundle: nil)

        searchViewController.popoverPresentationController?.delegate = self
        searchViewController.modelController.delegate = self
}

if self.searchViewController == nil {
        self.searchViewController = ASPatientSearchViewController(nibName: ASPatientSearchViewController.className, bundle: nil)
        
    }
    self.searchViewController.popoverPresentationController?.delegate = self
    self.searchViewController.modelController.delegate = self