Ios ViewController不符合UISwipegestureRecognitor的协议AnyObject错误
我在尝试执行刷卡手势时出现以下错误:Ios ViewController不符合UISwipegestureRecognitor的协议AnyObject错误,ios,swift,Ios,Swift,我在尝试执行刷卡手势时出现以下错误: var swipeGesture = UISwipeGestureRecognizer(target: self, action: slideToRightWithGestureRecognizer) func slideToRightWithGestureRecognizer() { } 我的ViewController如下所示: class ViewController: UIViewController, UIGestureRecognizerD
var swipeGesture = UISwipeGestureRecognizer(target: self, action: slideToRightWithGestureRecognizer)
func slideToRightWithGestureRecognizer() {
}
我的ViewController如下所示:
class ViewController: UIViewController, UIGestureRecognizerDelegate {
}
我在
target:self
上遇到错误,我是否必须实现一些委托,或者还有什么问题?我假设swipgesture
是一个属性-在这种情况下,问题是您在属性初始化期间引用了self
,这是不允许的,因为在初始化所有属性之前,self
不可用
我认为最好的解决方法是在viewDidLoad
方法中初始化属性,但当然属性必须是可选的-我认为在这种情况下使用隐式展开的属性是安全的:
var swipeGesture: UISwipeGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
self.swipeGesture = UISwipeGestureRecognizer(target: self, action: "slideToRightWithGestureRecognizer")
}
将参数添加到类型为
UISWeepGestureRecognizer
的slideToRightWithGestureRecognizer
。例如,func SlideToRightWithGestureRecognitzer(发送方:UISweepGestureRecognitzer){
我也尝试过,但没有效果您的错误消息是什么?ViewController->()->ViewController!不符合协议“AnyObject”。您需要在SlideToRightWithGestureRecognitizer
周围加引号。除此之外,不知道……旁注:这是一个典型的无意义编译错误消息示例。类似的错误消息有很多。。。