[英]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.