簡體   English   中英

無法構建'DepartmentDataDelegate',因為它沒有可訪問的初始化程序

[英]'DepartmentDataDelegate' cannot be constructed because it has no accessible initializers

我正在使用協議和委托將api數據保存到委托方法和另一個類中,以進行提取。 但是在第二類中,當我聲明此屬性時。 它顯示一條錯誤消息。

無法構建'DepartmentDataDelegate',因為它沒有可訪問的初始化程序

A類:添加用於將api數據保存到委托方法中的協議。

protocol DepartmentDataDelegate {
    func showDepttData(departments: [String : Any])
}

var delegate: DepartmentDataDelegate?

將api數據存儲到協議方法中

do {
                //create json object from data
                if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: [Any]] {
                    //print("POST Method :\(json)")


                    DispatchQueue.main.async {

                        for eachDepartment in json["departments"]!
                        {
                            let eachData = eachDepartment as! [String: Any]

                            self.delegate?.showDepttData(departments: eachData)
                        }
                        self.tableView.reloadData()
                    }

                    // handle json...
                }
            } catch let error {
                print(error.localizedDescription)
}

B類:該類正在獲取部門數據並在此處打印。

class ShowEmpVC: UIViewController, DepartmentDataDelegate {

    //Department Data Delegate
    var depttDelegate = DepartmentDataDelegate()

    var depttData = [String : Any]()

    override func viewDidLoad() {
        super.viewDidLoad()

        depttDelegate = self
        print("Departmens  are  :   \(depttData)")
    }
}

在您的協議中沒有init()函數。 因此,您不要調用DepartmentDataDelegate() 嘗試這個:

A類:

protocol DepartmentDataDelegate {
    func showDepttData(departments: [String : Any])
}

static var delegate: DepartmentDataDelegate?

B級:

class ShowEmpVC: UIViewController, DepartmentDataDelegate {

    var depttData = [String : Any]()

    override func viewDidLoad() {
        super.viewDidLoad()

        depttDelegate = self
        print("Departmens  are  :   \(depttData)")
    }

   override func showDepttData(departments: [String : Any]){
      // code
  }
}

我不確定是否需要override鍵。

協議類別

import UIKit

protocol sampleProtocol : class {
    func getValues(valuess:String)
}

class sampleClass {
    /// Shared Instance - So without creating a new Instance i  can make use of its function
    static let shared = sampleClass()
    /// Delegate Object
    var delegate : sampleProtocol?

    /// Sample Func which will send data using protocol
    func sendData(){
        /// Called whnen data is to be Transmitted
        delegate?.getValues(valuess: "output")
    }
}

用法-目標類-您的案例ShowEmpVC

import UIKit

class sampleVC: UIViewController
{
    override func viewDidLoad() {
        super.viewDidLoad()
        /// Make use of Shared Instance Created so, You need not to Re-allocate a New instance just to make use of Delegate
        sampleClass.shared.delegate = self
    }
}

/// Assign class to Protocol
extension sampleVC : sampleProtocol
{
    /// Protocol stub
    func getValues(valuess: String) {
        /// Get Value
        print(valuess)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM