如何在Swift中通过send multipeersession发送[Double]数组?

时间:2019-02-16 20:03:45

标签: ios arrays swift send

我有var列表:[Double] = [1.02,0.97,...],我正尝试将其发送给会话中的对等方。

我使用的是Apple提供的模板,因此他们已经设置了接收数据功能和sendtoallpeers()函数(如下):

func sendToAllPeers(_ data: Data) {
        do {
            try session.send(data, toPeers: session.connectedPeers, with: .reliable)
        } catch {
            print("error sending data to peers: \(error.localizedDescription)")
        }
    }

接收到的数据处理程序如下:

func receivedData(_ data: Data, from peer: MCPeerID) {

    do {

        if let worldMap = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: data) {

        } else
            if let anchor = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARAnchor.self, from: data) {


        } else if let list = try NSKeyedUnarchiver.unarchivedObject(ofClass: [Double].self, from: data) {




        }

最后一部分失败了,说:“调用中的参数标签不正确(具有'ofClass:from:',预期的'ofClasses:from:')”

Apple如何构造worldMap和anchor的发送函数如下:

let anchor = ARAnchor(name: "basicBoard", transform: hitTestResultForLater!.worldTransform)
            guard let data = try? NSKeyedArchiver.archivedData(withRootObject: anchor, requiringSecureCoding: true)
                else { fatalError("can't encode anchor") }
            self.multipeerSession.sendToAllPeers(data)

我如何发送列表:

guard let data2 = try? NSKeyedArchiver.archivedData(withRootObject: list, requiringSecureCoding: true)
        else { fatalError("can't encode list") }
    self.multipeerSession.sendToAllPeers(data2)

我不知道自己在做什么错。我整天都在网上搜索答案,但找不到答案。有人可以向我解释通过多对等会话发送数组的正确方法吗?

0 个答案:

没有答案