为什么会出现"unrecognized selector sent to class"错误?

11

我一直在查看所有的Stack Overflow,试图解决这个问题,但是没有任何解决方案有效。

class ToastView: UIView {

    static func showInParent(parentView: UIView!, withText text: String, forDuration duration: double_t) {

        //Count toast views are already showing on parent. Made to show several toasts one above another
        var toastsAlreadyInParent = 0;

        for view in parentView.subviews {
            if (view.isKindOfClass(ToastView)) {
                toastsAlreadyInParent++
            }
        }

        var parentFrame = parentView.frame;

        var yOrigin = parentFrame.size.height - getDouble(toastsAlreadyInParent)

        var selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, toastHeight);
        var toast = ToastView(frame: selfFrame)

        toast.textLabel.backgroundColor = UIColor.clearColor()
        toast.textLabel.textAlignment = NSTextAlignment.Center
        toast.textLabel.textColor = UIColor.whiteColor()
        toast.textLabel.numberOfLines = 2
        toast.textLabel.font = UIFont.systemFontOfSize(13.0)
        toast.addSubview(toast.textLabel)

        toast.backgroundColor = UIColor.darkGrayColor()
        toast.alpha = 0.0;
        toast.layer.cornerRadius = 4.0;
        toast.textLabel.text = text;

        parentView.addSubview(toast)
        UIView.animateWithDuration(0.4, animations: {
            toast.alpha = 0.9
            toast.textLabel.alpha = 0.9
        })

        var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self , selector: "hideSelf:", userInfo: nil,     repeats: false)
        //toast.performSelector(Selector("hideSelf"), withObject: nil, afterDelay: duration)

    }

    static private func getDouble(toastsAlreadyInParent : Int) -> CGFloat {
        return (70.0 + toastHeight * CGFloat(toastsAlreadyInParent) + toastGap * CGFloat(toastsAlreadyInParent));
    }

    func hideSelf( timer: NSTimer) {
        UIView.animateWithDuration(0.4, animations: {
            self.alpha = 0.0
            self.textLabel.alpha = 0.0
            }, completion: { t in self.removeFromSuperview() })
    }

} 

以下是我收到的错误信息:

NSInvalidArgumentException,原因:'+[HonorsApp.ToastView hideSelf:]:向类0x10b564530发送了未识别的选择器' *** 第一次抛出调用堆栈:

我已经尝试为从选择器调用的方法和类添加@objc,但都没有用。

另外:

Selector("hideSelf")

声明方法为hideSelf()
Selector("hideSelf:")

将方法声明为hideSelf(timer:NSTimer)

还应该检查该类是否从NSObject继承,这可以通过从UIView继承来实现,如果我没有错误的话。

我使用的是XCode 6.4swift 1.2

我刚开始学习swift编程,需要像android中的Toast函数一样的东西,我找到了这段代码,但是当我点击运行时出现了错误。

提前感谢您的帮助。


你使用的是哪个版本的Swift? - Daniel Ormeño
非常感谢,那正是我需要的。 - luis enrique mesias flores
4个回答

18

由于showInParent是一个static函数,在NSTimer.scheduledTimerWithTimeInterval中使用的self指的是类而不是类的实例。如果没有找到正确的目标,iOS将无法找到实例方法hideSelf

尝试将您创建的ToastView实例传递给计时器:

var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: toast, selector: "hideSelf:", userInfo: nil, repeats: false)

不经思考,我把添加通知的addObserver方法放进了一个类方法里,没有好好想一下。后来在其他地方添加和编辑了很多addObserver和postNotification之后,发现我的异常处理被触发了,而NSNotification观察者却没有被触发。我以为是方法名上下文冲突,进行了一些调试...然后发现了这个问题,用已故的大师詹姆斯·布朗的话说:“天哪!” :-) 谢谢你! - Cerniuk

1
我今天早些时候遇到了这个错误。Xcode建议加入以下内容:

self' refers to the method 'LoginController.self', which may be unexpected Use 'LoginController.self' to silence this warning

但事实证明,LoginController.self不起作用。这是Xcode中的一个错误。

1
不不不。编译器发出警告是因为self会变得不寻常。它告诉你如何以一种看起来是有意的方式来编写它,以便消除警告。但是self一直都是\错误的\,所以在说服编译器你的意思之后,编译器发现你一直都是错的。 - gnasher729

0

尝试这段代码。

toast.performSelector(Selector("hideSelf:"), withObject: nil) 或者

toast.performSelector(#selector(ClassName.hideSelf(_:)), withObject: nil) s

0

修改此行:

var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self , selector: "hideSelf:", userInfo: nil, repeats: false)

转换为:

var timer = NSTimer.scheduledTimerWithTimeInterval(duration, target: toast , selector: "hideSelf:", userInfo: nil, repeats: false)

这应该会有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接