Liyuan Liu Liyuan Liu - 4 months ago 11
Swift Question

Cannot invoke 'sendData' with an argument list of type '(NSData,toPeer:NSArray,......'

I am new to

MultipeerConnectivity
, when I am implementing my custom
sendData
function, it shows
Cannot invoke 'sendData' with an argument list of type '(NSData,toPeer:NSArray,withMode:MCSessionSendDataMode',error:inout NSError?)


How to fix that:

func sendData(dataToSend:dataSend,toPeer: MCPeerID)->Bool{
let dataToSend = NSKeyedArchiver.archivedDataWithRootObject(dataToSend)
let peerArray = NSArray(object: toPeer)
var error:NSError?

let arr = [MCPeerID]()
//this line shows error
if !self.session!.sendData(dataToSend, toPeers: peerArray, withMode: MCSessionSendDataMode.Reliable, error: &error) {

}
}

Answer
if !self.session!.sendData(dataToSend, toPeers: peerArray, withMode: MCSessionSendDataMode.Reliable, error: &error) {

}

This method doesn't return a Boolean in Swift 2.x

This is the method from the documentation from Apple

func sendData(_ data: NSData,
      toPeers peerIDs: [MCPeerID],
     withMode mode: MCSessionSendDataMode) throws

This means do you have to something like this

do {
    try self.session!.sendData(dataToSend, toPeers: peerArray, withMode: MCSessionSendDataMode.Reliable)
    print("success")
    // do something
} catch {
    print("failure")
    // do something else
}

Tested on Swift 2 and Xcode 7.3.1