无法将数据传递给具有委派的标签

时间:2018-05-04 03:47:00

标签: swift4

我的控制台打印:" ModelToControllerDelegate.Model"。

导入基金会

class Model {     let firstName:String     let lastName:String

init(firstName: String, lastName: String) {
    self.firstName = firstName
    self.lastName = lastName
}

}

导入基金会

协议DataModelDelegate:class {     func didRecievedDataUpdate(数据:模型) }

class ModelController {

weak var delegate: DataModelDelegate?

func requestData() {
    let data = Model(firstName: "Jack", lastName: "Johnson")
    delegate?.didRecievedDataUpdate(data: data)

}

}

导入UIKit

类ViewController:UIViewController {

let modelController = ModelController()

@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    modelController.delegate = self
    modelController.requestData()
}

}

扩展名ViewController:DataModelDelegate {     func didRecievedDataUpdate(data:Model){         打印(数据)     } }

1 个答案:

答案 0 :(得分:0)

添加

    extension ViewController: DataModelDelegate { 
     func didRecievedDataUpdate(data: Model) { 
     print(data) // data is object of class Model so output is"ModelToControllerDelegate.Model"
     print(data.firstName) // firstName is String so Output is Jack
     print(data.lastName)  // lastName is String so Output is Johnson
     } 
    }