【发布时间】:2019-03-13 23:45:54
【问题描述】:
以下是我的代码,我正在关注在线教程,但出现上述错误。
import UIKit
import MultipeerConnectivity
class ViewController: UIViewController, MCBrowserViewControllerDelegate {
@IBOutlet var xField: [MyImageView]!
var appDelegate:AppDelegate!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate.mpcHandling.gettingPeerConnection(displayName: UIDevice.current.name)
appDelegate.mpcHandling.gettingSession()
appDelegate.mpcHandling.showingSelf(show: true)
//handling peer state notification
//getting error for the below mentioned line
NotificationCenter.default.addObserver(self, selector: #selector("peerChangedNotification:"), name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil)
NotificationCenter.default.addObserver(self, selector: Selector("handleReceivedNotification:"), name: NSNotification.Name(rawValue: "MPC_DidReceiveNotification"), object: nil)
mainLogicField()
}
@objc func peerChangedNotification(notification:NSNotification){
let userInfo = NSDictionary(dictionary: notification.userInfo!)
let state = userInfo.object(forKey: "state") as! Int
if state != MCSessionState.connecting.rawValue{
//Now we inform user that we have connected
self.navigationItem.title = "Connected Successfully"
}
}
@IBAction func getConnected(_ sender: Any) {
if appDelegate.mpcHandling.gameSession != nil {
appDelegate.mpcHandling.gettingBrowser()
appDelegate.mpcHandling.browser.delegate = self
self.present(appDelegate.mpcHandling.browser, animated: true, completion: nil
)
}
}
func mainLogicField() {
for index in 0 ... xField.count - 1 {
let recognizeGestureTouch = UITapGestureRecognizer(target: self, action: "fieldTapped")
recognizeGestureTouch.numberOfTapsRequired = 1
xField[index].addGestureRecognizer(recognizeGestureTouch)
}
}
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
appDelegate.mpcHandling.browser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
appDelegate.mpcHandling.browser.dismiss(animated: true, completion: nil)
}
}
不知道出了什么问题,因为我也使用了#selector 和@objc 作为我的函数。 网上教程刚刚用到了 NotificationCenter.default.addObserver(self, selector: "peerChangedNotification:", name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil) 没有添加#selector,但如果我删除#selector,它会给我一个警告,指出“没有使用Objective-C选择器'peerChangedNotification'声明的方法”。 我正在尝试使用 mutipeer 连接 2 台设备
【问题讨论】:
-
您使用的选择器语法错误。
-
提示:
#selector不带字符串。