Swift 2无法识别的选择器处于手势状态 [英] Swift 2 unrecognized selector in tapgesture

查看:94
本文介绍了Swift 2无法识别的选择器处于手势状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始将我的应用程序更新为Swift 2.0,但是我遇到了一个问题,该问题在SO上有一百种不同的答案,似乎都与我的问题无关.

I recently started to update my app to Swift 2.0, but I've run into a problem that has a hundred different answers on SO, none of which seemed to be related to my problem.

(在将应用程序更新为Swift 2.0之前,此方法可以工作),但是我无法找到对点击手势识别器所做的任何更改?

(This worked before updating the application to Swift 2.0), but I havn't been able to find any changes made to tap gesture recognisers?

这是我得到的完整错误:

Here's the full error I'm getting:

[Stocks.Sid​​ePanelViewController proFormulaTapGesture]:无法识别的选择器已发送到实例0x14f06ae00

[Stocks.SidePanelViewController proFormulaTapGesture]: unrecognized selector sent to instance 0x14f06ae00

由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:-[Stocks.Sid​​ePanelViewController proFormulaTapGesture]:无法识别的选择器已发送到实例0x14f06ae00"

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Stocks.SidePanelViewController proFormulaTapGesture]: unrecognized selector sent to instance 0x14f06ae00'

***首先抛出调用堆栈: (0x1830904d0 0x197afff9c 0x1830971ec 0x183094188 0x182f9920c 0x188c3c58c 0x188899b50 0x18872d104 0x188c3bc30 0x1886ed28c 0x1886eb3ac 0x18872b088 0x18872a5ec 0x1886fb908 0x1886f9fac 0x183047d6c 0x183047800 0x183045500 0x182f75280 0x18e0ec0cc 0x188762df8 0x100158184 0x1983428b8) libc ++ abi.dylib:以类型为NSException的未捕获异常终止

*** First throw call stack: (0x1830904d0 0x197afff9c 0x1830971ec 0x183094188 0x182f9920c 0x188c3c58c 0x188899b50 0x18872d104 0x188c3bc30 0x1886ed28c 0x1886eb3ac 0x18872b088 0x18872a5ec 0x1886fb908 0x1886f9fac 0x183047d6c 0x183047800 0x183045500 0x182f75280 0x18e0ec0cc 0x188762df8 0x100158184 0x1983428b8) libc++abi.dylib: terminating with uncaught exception of type NSException

这是一个简单的轻击手势.但这似乎再也无法识别选择器了.

It's a simple tap gesture. But it seems to not recognize the selector anymore.

这是我用来设置识别器的代码:

Here's the code I use to set up the recognizer:

let proFormulaTap = UITapGestureRecognizer()

proFormulaTap.addTarget(self, action:"proFormulaTapGesture")
proFormulaView.addGestureRecognizer(proFormulaTap)

这是我要运行的功能:

func proFormulaTapGesture() throws {
        print("proFormulaTapGesture")
        selectView(proFormulaView)
        selectedMenuItem = 0
        Formula = 1
        menuTabBarController.selectedIndex = 0
        navigationController?.navigationBar.topItem!.title = "BASIC FORMULA"
        try (menuTabBarController.viewControllers as! [SuggestionsViewController])[0].loadSuggestions()
    }

但是,由于它从不打印"proFormulaTapGesture",在控制台中.我绝对认为该错误发生在函数之前.错误还提到了选择器.

However since it never prints "proFormulaTapGesture" in the console. I definitely think the error occurs before the function. Also suggested by the error mentioning the selector.

自从更新到Swift 2.0以来,显然已在函数中添加了try/catch,但是tapGestureRecognizer的设置没有任何变化.

Obviously try/catch has been added to the function since the update to Swift 2.0, but nothing has been changed in the setup of the tapGestureRecognizer.

我尝试删除投掷"并尝试使用该功能,但问题仍然存在.我还尝试过制作按钮而不是点击手势识别器.但是我仍然遇到相同的错误,表明选择器(功能)可能是问题,而不是点击手势/按钮.但是我的应用程序中的所有其他按钮都可以正常工作吗?

I tried removing the "throws" and try from the function, but the problem perists. I also tried making a button instead of a tap gesture recognizer. But I'm still getting the same error, suggesting it might be a problem with the selector (function) and not the tap gesture / button. However all my other buttons in my app works just fine?

我还尝试重命名选择器/功能和轻击手势识别器.还是一样.

I also tried to rename the selector/function, and the tap gesture recognizer. Still the same.

原始代码是用Swift而不是Obj-C编写的.在Apple将代码转换为Swift 2.0的过程中添加了"throws and try"

The original Code was programmed in Swift, not Obj-C. The throws and try, was added during Apple's code conversion to Swift 2.0

任何对为什么此突然中断的帮助将不胜感激.

Any help to why this is suddenly broken would be greatly appreciated.

谢谢!

推荐答案

问题可能是,至少在这种情况下,UIKit不知道如何调用选择器throws.

The issue may be that UIKit does not know how to call a selector that throws, at least in this context.

请记住,UIKit将在响应轻击动作时调用此函数.不习惯调用throws函数;显然,就Objective-C运行时而言,throw必须更改方法的调用签名.除此之外,如果您的函数proFormulaTapGesture确实引发了错误,那么即使UIKit可以捕获到该错误,它也会如何处理呢?

Remember that UIKit will be calling this function in response to a tap action. It is not accustomed to calling a function that throws; obviously the throw must alter the method's call signature as far as the Objective-C runtime is concerned. Beyond that, if your function proFormulaTapGesture did throw an error, what would UIKit do with the error even if it could catch it?

我不确定您在哪里添加了do/try/catch.但是,除非它在您的动作函数proFormulaTapGesture中,否则我看不到它的重要性.我相信您将需要在proFormulaTapGesture内部实现try/catch错误处理,以便有效地吞并错误,从而不会抛出proFormulaTapGesture.

I'm not sure where you added do/try/catch. But unless it was inside your action function proFormulaTapGesture, I do not see how it could be relevant. I believe that you will need to implement the try/catch error handling inside proFormulaTapGesture, so that it effectively swallows the error, so that proFormulaTapGesture does not throw.

我自己创建了一个快速测试项目,发现throw ing目标操作会产生与您相同的无法识别的选择器"错误:

I just created a quick test project myself and found that a throwing target action gives the same "Unrecognized selector" error you got:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tapper = NSClickGestureRecognizer(target: self, action: "didClick")
    self.view.addGestureRecognizer(self.tapper!)
}

func didClick() throws {
    print("did click")
}

2015-07-27 00:16:43.260 Test1 [71047:54227175]-[Test1.ViewController didClick]:无法识别的选择器已发送到实例0x6000000c5010

2015-07-27 00:16:43.260 Test1[71047:54227175] -[Test1.ViewController didClick]: unrecognized selector sent to instance 0x6000000c5010

...

我相信您将需要对代码进行重新处理以在操作函数中内部处理该错误,以便它不会引发-像这样的东西:

I believe you will need to rework your code to handle the error internally in your action function so that it does not throw - something like this:

func proFormulaTapGesture() {
    print("proFormulaTapGesture")
    selectView(proFormulaView)
    selectedMenuItem = 0
    Formula = 1
    menuTabBarController.selectedIndex = 0
    navigationController?.navigationBar.topItem!.title = "BASIC FORMULA"
    do {
        try (menuTabBarController.viewControllers as! [SuggestionsViewController])[0].loadSuggestions()
    } catch {
        // Handle the error here somehow
    }
}

如果您无法处理proFormulaTapGesture中的错误,则必须将其从函数中传递出去(例如,NSNotification等),从而更具创造力.

If you are unable to handle the error within proFormulaTapGesture, you'll have to be more creative with passing it out of the function (think NSNotification, etc...).

为清楚起见,我在Xcode 7 beta 4中创建了一个新的Single View iOS应用程序,并将此确切的代码用作ViewController.没有其他变化.该应用程序可以在iOS模拟器中编译并正常运行.

To be clear, I created a new Single View iOS application in Xcode 7 beta 4, and used this exact code as my ViewController. No other changes. The app compiles and works just fine in the iOS simulator.

@robertvojta还指出,如果下面的didTap标记为private,则还需要@objc以避免动态调度崩溃.

@robertvojta also points out that if didTap below is marked private, @objc is also required to avoid a dynamic dispatch crash.

import UIKit

class ViewController: UIViewController {

    var tapper: UITapGestureRecognizer?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tapper = UITapGestureRecognizer(target: self, action: "didTap")
        self.view.addGestureRecognizer(self.tapper!)
    }

    func didTap() {
        print("did Tap")
    }

}

(请注意,我也在OS X项目上进行了测试,这是我早先在答案中使用的代码).

(Note I also tested on an OS X project, which is the code I used earlier in the answer).

这篇关于Swift 2无法识别的选择器处于手势状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆