ios - 尝试使用 NSTimer 并在运行我的应用程序时发现一个奇怪的错误

标签 ios swift debugging nstimer

我在控制台中收到此错误,但我不知道如何修复它或它是什么。

2015-06-30 21:09:28.268 stop watch[1394:373417] -[stop_watch.ViewController 

result]: unrecognized selector sent to instance 0x7fa7f0513df0

2015-06-30 21:09:28.270 stop watch[1394:373417] *** Terminating app due to 

uncaught exception 'NSInvalidArgumentException', reason: '-

[stop_watch.ViewController result]: unrecognized selector sent to instance 
0x7fa7f0513df0'

*** First throw call stack:

(

0   CoreFoundation                      0x000000010bee7a75 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010da3fbb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010beeed1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x000000010be469dc ___forwarding___ + 988
4   CoreFoundation                      0x000000010be46578 _CF_forwarding_prep_0 + 120
5   Foundation                          0x000000010c31f844 __NSFireTimer + 83
6   CoreFoundation                      0x000000010be4f6c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
7   CoreFoundation                      0x000000010be4f285 __CFRunLoopDoTimer + 1045
8   CoreFoundation                      0x000000010be1259d __CFRunLoopRun + 1901
9   CoreFoundation                      0x000000010be11bc6 CFRunLoopRunSpecific + 470
10  GraphicsServices                    0x0000000110003a58 GSEventRunModal + 161
11  UIKit                               0x000000010c77a580 UIApplicationMain + 1282
12  stop watch                          0x000000010bd067be top_level_code + 78
13  stop watch                          0x000000010bd067fa main + 42
14  libdyld.dylib                       0x000000010e21b145 start + 1
)

   libc++abi.dylib: terminating with uncaught exception of type NSException
   (lldb) 

抱歉,我希望我能说得更清楚一点,但我真的不知道问题出在哪里。

另外,我不知道这是否有帮助,但我会将我正在运行的代码放在下面。

import UIKit

class ViewController: UIViewController {

    var timer = NSTimer()
    var count = 0
    var condition = 1

    @IBOutlet weak var main: UILabel!
    @IBOutlet weak var stopOut: UIButton!

    @IBAction func start(sender: AnyObject) {

        func result() {
            count++
        }

        condition = 1
        main.text = String(count)
    }

    @IBAction func stop(sender: AnyObject) {

        if condition == 1 {
            timer.invalidate()
            condition = 0
            stopOut.setTitle("stop", forState: UIControlState.Normal)

        }

        else {

            stopOut.setTitle("clear", forState: UIControlState.Normal)
            timer.invalidate()
            count = 0
            main.text = String(count)
            condition = 1
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

所以我最近做了一些更改,我将结果函数从启动函数中取出,现在它启动了,但是现在当我按下计时器上的启动按钮时,我需要连续按下启动按钮才能更新。那么我怎样才能使它只在按下按钮时才开始滴答作响,并且每秒继续更新到下一个数字。

最佳答案

在您的 ViewController 类中,您的 viedDidLoad() 方法应更改为以下内容(删除 Selector,请参阅 this Stack Overflow answer 了解更多信息信息):

override func viewDidLoad() {
    super.viewDidLoad()
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "result", userInfo: nil, repeats: true)
}

然后,将 result() 声明从名为 start@IBAction 中移出。也就是说,在 ViewController 内部声明它,如下所示:

import UIKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        // ...
    }

    // ... other functions here

    func result() {
        self.count++
    }
}

将来,每当您看到表单错误

[someObject someFunction]: unrecognized selector sent to instance ...

您应该首先检查以确保 someObject 声明了一个名为 someFunction 的函数。这几乎总是问题所在。

<小时/>

为了响应您的编辑,您应该像这样实现 result():

func result() {
    self.count++
    main.text = "\(self.count)"
}

这将导致每次计时器触发时更新 UILabel 中的文本。

关于ios - 尝试使用 NSTimer 并在运行我的应用程序时发现一个奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152480/

相关文章:

iphone - 具有两个 NSFetchedResultsController 实例的 TableView

ios - 如何在 swift 的辅助功能中使用点击手势?

sql - 有没有更好的方法来调试 SQL?

arrays - 如何制作一个以二维数组为值的字典?

swift - 处理 AlamoFire 中异步通信导致的延迟

java - Eclipse 在 Debug模式下不会在断点处停止

c++ - 在 MSVS 调试器中导出扩展的 std::vector 转储

iOS JPEG 图像旋转 90 度

ios - 以编程方式设置为 Root View Controller 时, View Controller 无法正确显示 subview

ios - 将按钮数组添加到适当的表行