简体   繁体   English

无法构建'DepartmentDataDelegate',因为它没有可访问的初始化程序

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

I am using Protocol and Delegates for saving api data into delegate method and in another class, fetching it. 我正在使用协议和委托将api数据保存到委托方法和另一个类中,以进行提取。 But in second class, when i declared this property. 但是在第二类中,当我声明此属性时。 It shows an error message. 它显示一条错误消息。

'DepartmentDataDelegate' cannot be constructed because it has no accessible initializers 无法构建'DepartmentDataDelegate',因为它没有可访问的初始化程序

Class A : Add protocol for saving api data into the delegate method. A类:添加用于将api数据保存到委托方法中的协议。

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

var delegate: DepartmentDataDelegate?

Stored api data into protocol method 将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)
}

Class B : This class is fetching the department data and print here. 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)")
    }
}

In your protocol not have a init() func. 在您的协议中没有init()函数。 So you not call DepartmentDataDelegate() . 因此,您不要调用DepartmentDataDelegate() Try this: 尝试这个:

Class A: A类:

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

static var delegate: DepartmentDataDelegate?

Class B: 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
  }
}

I not sure if needs override key. 我不确定是否需要override键。

Protocol class 协议类别

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

Usage -- Destination Class -- Your case ShowEmpVC 用法-目标类-您的案例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.

相关问题 无法构造“任务”,因为它没有可访问的初始值设定项 - 'Task' cannot be constructed because it has no accessible initializers 无法构造&#39;UITabBarDelegate&#39;,因为它没有可访问的初始化程序 - 'UITabBarDelegate' cannot be constructed because it has no accessible initializers 错误:'结果<data, foursquareerror> ' 无法构造,因为它没有可访问的初始值设定项</data,> - Error: 'Result<Data, FourSquareError>' cannot be constructed because it has no accessible initializers 为什么我在 Swift 中得到这个“无法构造,因为它没有可访问的初始值设定项” - Why do I get this in Swift “cannot be constructed because it has no accessible initializers” Swift:为什么使用MKPointAnnotation而不使用MKAnnotation。 无法构造“ MKAnnotation”错误,因为它没有可访问的初始化程序。 - Swift: why MKPointAnnotation and not MKAnnotation. Errors out with “'MKAnnotation' cannot be constructed because it has no accessible initializers” 无法构造枚举,因为它没有可访问的初始化程序 - enum cannot be constructed because it has no accessible initialisers 类&#39;ProductDetailViewController&#39;没有初始化程序 - Class 'ProductDetailViewController' has no initializers “ViewController”类没有初始值设定项 - Class 'ViewController' has no initializers 类没有初始化程序Swift - Class has no initializers Swift 类“视图控制器”没有初始化程序 - Class “View controller” has no initializers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM