简体   繁体   English

IOS swift 4 中的点对点连接

[英]Peer to Peer Connection in IOS swift 4

I need help with this code.我需要有关此代码的帮助。 I followed a tutorial from 2014 and I have an error with dispatch_async.我遵循了 2014 年的教程,但 dispatch_async 出现错误。 I am trying to create an app that allows the user to play with other player nearby or peer to peer.我正在尝试创建一个应用程序,允许用户与附近的其他玩家或对等玩家一起玩。 I think the code needs to be updated because it has been renamed in swift 4 but I don't know what the new name is.我认为代码需要更新,因为它已在 swift 4 中重命名,但我不知道新名称是什么。

This is the code这是代码

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

the full code is in this link https://github.com/javaplanet17/test/blob/master/multipeer it is inside of a func on line 36.完整代码在此链接中https://github.com/javaplanet17/test/blob/master/multipeer它位于第 36 行的 func 内。

the error list first I got this error错误列表首先我得到了这个错误

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

I pressed fix and renamed it then I got another error我按下了修复并重命名了它然后我又遇到了另一个错误

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

I pressed fix and renamed it then I still got an error我按下修复并重命名它然后我仍然遇到错误

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

Once again I pressed fix and changed it我再次按下修复并更改它

The code now look like this:代码现在看起来像这样:

NotificationCenter.defaultCenter.postNotificationName("MPC_DidChangeStateNotification",object:nil,userInfo: userinfo)

Then I updated it to:然后我将其更新为:

dispatch_async(dispatch_get_main_queue(),{ () -> Void in
            NotificationCenter.default.post("MPC_DidChangeStateNotification",object:nil,userInfo: userinfo)
        })

yet I still got an error但我仍然遇到错误

MPCHandler.swift:55:121: Extra argument 'userInfo' in call

I have tried to change it into:我试图将其更改为:

DispatchQueue.main.async(execute:{ () -> Void in NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil, userInfo: userinfo))
        })

but I still get an error.但我仍然收到错误消息。 How do I fix this error?我该如何解决这个错误?

It worked for me this way might try it out这种方式对我有用,可以试试

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