【问题标题】:'DepartmentDataDelegate' cannot be constructed because it has no accessible initializers'DepartmentDataDelegate' 无法构造,因为它没有可访问的初始化程序
【发布时间】:2018-09-30 18:19:42
【问题描述】:

我正在使用协议和委托将 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)")
    }
}

【问题讨论】:

  • 您实际上无法构造它,您需要创建对声明协议的类 A 的引用 在类 b 中,只需使用该类 A 引用将委托设置为 self
  • 我是第一次使用,你能解释一下如何使用吗?
  • 在您的Class B 中,您会执行类似var obj = ClassA() 然后obj.delegate = self 的操作
  • 这个查询解决了吗?

标签: ios swift api delegates protocols


【解决方案1】:

在您的协议中没有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 密钥。

【讨论】:

    【解决方案2】:

    协议类

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      相关资源
      最近更新 更多