我是MultipeerConnectivity
的新用户,当我实施自定义sendData
功能时,它会显示Cannot invoke 'sendData' with an argument list of type '(NSData,toPeer:NSArray,withMode:MCSessionSendDataMode',error:inout NSError?)
如何解决这个问题:
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) {
}
}
答案 0 :(得分:1)
if !self.session!.sendData(dataToSend, toPeers: peerArray, withMode: MCSessionSendDataMode.Reliable, error: &error) {
}
此方法在Swift 2.x
中不返回布尔值这是来自Apple
的文档中的方法func sendData(_ data: NSData,
toPeers peerIDs: [MCPeerID],
withMode mode: MCSessionSendDataMode) throws
这意味着你必须这样吗
do {
try self.session!.sendData(dataToSend, toPeers: peerArray, withMode: MCSessionSendDataMode.Reliable)
print("success")
// do something
} catch {
print("failure")
// do something else
}
在Swift 2和Xcode 7.3.1上测试