IOS swift 4中的对等连接
我需要这个代码的帮助。我学习了2014年的一个教程,发现dispatch_async有一个错误。我正在尝试创建一个应用程序,允许用户与附近的其他玩家或点对点进行游戏。我认为代码需要更新,因为它已在swift 4中重命名,但我不知道新名称是什么 这是密码IOS swift 4中的对等连接,ios,swift,Ios,Swift,我需要这个代码的帮助。我学习了2014年的一个教程,发现dispatch_async有一个错误。我正在尝试创建一个应用程序,允许用户与附近的其他玩家或点对点进行游戏。我认为代码需要更新,因为它已在swift 4中重命名,但我不知道新名称是什么 这是密码 // below is where the code are wrong dispatch_async(dispatch_get_main_queue(),{ () -> Void in NSNotifica
// below is where the code are wrong
dispatch_async(dispatch_get_main_queue(),{ () -> Void in
NSNotificationCenter.defaultCenter().postNotificationName("MPC_DidChangeStateNotification",object:nil,userInfo: userInfo)
})
完整代码在此链接中,它位于func的第36行
错误列表
首先我犯了这个错误
MPCHandler.swift:55:9: 'NSNotificationCenter' has been renamed to 'NotificationCenter'
我按了fix并重新命名了它,然后又出现了一个错误
MPCHandler.swift:55:123: Use of unresolved identifier 'userInfo'
我按了fix并重新命名了它,但仍然出现了一个错误
MPCHandler.swift:55:45: Cannot call value of non-function type 'NotificationCenter' Replace '()' with ''
MPCHandler.swift:55:121: Extra argument 'userInfo' in call
我再次按下fix键并更改了它
代码现在如下所示:
NotificationCenter.defaultCenter.postNotificationName("MPC_DidChangeStateNotification",object:nil,userInfo: userinfo)
然后我将其更新为:
dispatch_async(dispatch_get_main_queue(),{ () -> Void in
NotificationCenter.default.post("MPC_DidChangeStateNotification",object:nil,userInfo: userinfo)
})
但我还是犯了一个错误
MPCHandler.swift:55:45: Cannot call value of non-function type 'NotificationCenter' Replace '()' with ''
MPCHandler.swift:55:121: Extra argument 'userInfo' in call
我已尝试将其更改为:
DispatchQueue.main.async(execute:{ () -> Void in NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil, userInfo: userinfo))
})
但我还是犯了一个错误。如何修复此错误?它对我有效,这样我可以尝试一下
DispatchQueue.main.async(execute: { _ in
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil, userInfo: userInfo)
})
您的问题包括完整和准确的错误。您还有一个额外的
)
。以下是所有functionfunc会话(uSession:MCSession,peer peerID:MCPeerID,didChange state:MCSessionState){let userInfo=[“peerID”:peerID,“state”:state.rawValue]作为[String:Any]DispatchQueue.main.async(执行:{()->NotificationCenter.default.post中的Void(名称:NSNotification.name(rawValue:“MPC_DidChangeStateNotification”)、object:nil、userInfo:userInfo)})请使用,而不是在注释中添加信息。