#selector 的参数未引用“@objc”方法、属性 或初始化程序
Argument of #selector does not refer to an '@objc' method, property or initializer
下面是我的代码,我正在按照在线教程进行操作,但出现了上述错误。
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,那么它会给我一个警告,说明"No method declared with Objective-C selector 'peerChangedNotification'"。
我正在尝试使用 mutipeer
连接这两个设备
Selector() : 它接收字符串/字符串文字作为参数。以前用过,#selector
升级定义
#selector : 您正试图在 #selector
中传递一个字符串,而它需要 Objective-C 类型的函数引用。正确的例子是:
NotificationCenter.default.addObserver(self, selector: #selector(peerChangedNotification(notification:)), name: NSNotification.Name("MPC_DidChangeStateNotification"), object: nil)
下面是我的代码,我正在按照在线教程进行操作,但出现了上述错误。
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,那么它会给我一个警告,说明"No method declared with Objective-C selector 'peerChangedNotification'"。 我正在尝试使用 mutipeer
连接这两个设备Selector() : 它接收字符串/字符串文字作为参数。以前用过,#selector
升级定义
#selector : 您正试图在 #selector
中传递一个字符串,而它需要 Objective-C 类型的函数引用。正确的例子是:
NotificationCenter.default.addObserver(self, selector: #selector(peerChangedNotification(notification:)), name: NSNotification.Name("MPC_DidChangeStateNotification"), object: nil)