繁体   English   中英

IOS swift 4 中的点对点连接

[英]Peer to Peer Connection in IOS swift 4

我需要有关此代码的帮助。 我遵循了 2014 年的教程,但 dispatch_async 出现错误。 我正在尝试创建一个应用程序,允许用户与附近的其他玩家或对等玩家一起玩。 我认为代码需要更新,因为它已在 swift 4 中重命名,但我不知道新名称是什么。

这是代码

// below is where the code are wrong
        dispatch_async(dispatch_get_main_queue(),{ () -> Void in
        NSNotificationCenter.defaultCenter().postNotificationName("MPC_DidChangeStateNotification",object:nil,userInfo: userInfo)
        })

完整代码在此链接中https://github.com/javaplanet17/test/blob/master/multipeer它位于第 36 行的 func 内。

错误列表首先我得到了这个错误

MPCHandler.swift:55:9: 'NSNotificationCenter' has been renamed to 'NotificationCenter'

我按下了修复并重命名了它然后我又遇到了另一个错误

MPCHandler.swift:55:123: Use of unresolved identifier 'userInfo'

我按下修复并重命名它然后我仍然遇到错误

MPCHandler.swift:55:45: Cannot call value of non-function type 'NotificationCenter' Replace '()' with ''

我再次按下修复并更改它

代码现在看起来像这样:

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: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)
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM