【发布时间】:2016-11-02 07:37:26
【问题描述】:
我创建了一个对象,它有几个要发送的属性:1 个字符串(会话日期)、另一个对象的数组、1 个时间间隔和 1 个 Int(会话距离)。我使用多重连接发送此对象,并且设备注册它们正在接收数据。但是,未收到整数。它在接收设备上变为 nil。
感谢您的帮助!
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) { 切换状态{ 案例 MCSessionState.connected: print("已连接:(peerID.displayName)")
joinedSession = true
let hosting = ViewController().hostCheck()
print("InMCSessionConnected")
if(hosting == false){
}
if(hosting == true){
if(self.ConnectedUsers.text?.range(of: peerID.displayName) == nil){
DispatchQueue.main.async {
self.ConnectedUsers.text?.append(peerID.displayName + ", ")
}
}
HostingSession.noJoined = HostingSession.noJoined + 1;
print(HostingSession.noJoined)
print(SessionsManager.SessionInception.last?.SessionDistance)
print(SessionsManager.SessionInception.last?.SessionDate)
print("SendingData")
sendData(SessionsManager.SessionInception.last!)
print("SendingData")
}
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
joinedSession = false
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
//This is called when data arrives
print("Starting Received Data")
let hosting = ViewController().hostCheck()
if(hosting == false){
let SessionSent = (NSKeyedUnarchiver.unarchiveObject(with: data) as! Sessions)
print(SessionSent.SessionDistance)
SessionsManager.SessionInception.append(SessionSent)//appends the session sent
//2 method gets the current date and line below appends this date to title
let currentSession = SessionsManager.SessionInception.last
let currentSessionDate = (currentSession?.SessionDate)!
print(currentSessionDate)
reloadView()
//Creates the table
TableView.rowHeight = 90
TableView.delegate = self
TableView.dataSource = self
TableView.reloadData()
print("Finished Received Data")
//ReadyButton.enabled = true
}
}
【问题讨论】:
-
请将您的代码以文本形式发布
标签: swift multipeer-connectivity