使用委托方法在 2 类 之间发送数据

Sending data between 2 classes with Delegate Methods

我正在尝试通过 MultipeerConnectivity 在 2 classes 之间发送数据。我正在尝试通过委托方法来做到这一点。

我定义了这个协议:

protocol AssetDelegate {
    func changeThisName(data: Data)
}

我的主要 ViewController 是 AssetDelegate 并且简单地实现了函数:

func changeThisName(data: Data) {
    print("changeThisName called")
    if let image = UIImage(data: data) {
        imageViewAsset.image = image
    }
}

这只是为了查看接收到的图像是否可以解码并分配给 imageViewAsset UI 元素。

此方法在我定义的 MultipeerConnectivity "Manager" class 中调用,它在 AppDelegate 中有一个实例(这是 "singleton" 的定义吗?)

我的 "MPCManager" class 有各种 MCSession 委托方法,特别是 session(didReceive:data) 方法,即:

var assetDelegate: AssetDelegate?

func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
    print("Data Recieved: \(data)")
    self.assetDelegate?.changeThisName(data: data)
}

我在控制台日志中看到 print("Data Received:(data)")。但是我没有看到 changeThisName 方法的打印语句。

这是控制台日志输出。基本上有 2 行显示 MPC 连接("other state" 和 "session connected")。然后有 2 "Received Data" 项。

2019-08-14 12:42:29.714402+0100 My App[34860:11895498] [Accessibility] ****************** Loading GAX Client Bundle ****************
2019-08-14 12:42:44.121481+0100 My App[34860:11895563]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
other state...<MCPeerID: 0x281b541c0 DisplayName = SendDevice>
2019-08-14 12:42:45.493567+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.542647+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.586389+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.641138+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.730210+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.795905+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:45.968786+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
session connected... <MCPeerID: 0x281b541c0 DisplayName = SendDevice>
2019-08-14 12:42:46.037399+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:46.379153+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:46.444389+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:47.099950+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:47.158868+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:48.351072+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:48.390215+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:49.856339+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:49.922462+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
Data Recieved: 8269 bytes
2019-08-14 12:42:51.375239+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:51.454734+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:52.913337+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
2019-08-14 12:42:52.977319+0100 My App[34860:11895661]  [ERROR] ProcessEvent:1199   Send BINDING_REQUEST failed(C01A0041).
Data Recieved: 4569624 bytes
2019-08-14 12:42:54.271102+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [0].
2019-08-14 12:42:54.293615+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [1].
2019-08-14 12:42:54.306191+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.306387+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [2].
2019-08-14 12:42:54.313382+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.313444+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [3].
2019-08-14 12:42:54.318396+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.318453+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [5].
2019-08-14 12:42:54.323359+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
2019-08-14 12:42:54.323407+0100 My App[34860:11895531] [GCKSession] Not in connected state, so giving up for participant [4725ECEB] on channel [6].
2019-08-14 12:42:54.328377+0100 My App[34860:11895531]  [ERROR] ICEStopConnectivityCheck:2688 ICEStopConnectivityCheck() found no ICE check with call id (1193667819)
protocol AssetDelegate {
    func changeThisName(data: Data)
}

class A: AssetDelegate {
    func goToClassB(){
        var b = B()
        b.assetDelegate = self // <-- I don't see this line in your code
    }
}

class B {
    var assetDelegate: AssetDelegate?

    func passDataToA(){
        self.assetDelegate?.changeThisName(whatEverDataHere)
    }
}