0

I got this error in the console, and I have no idea how to fix it or what it is.

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) 

Sorry, I wish I could be a bit more clear, but I really don't know what the problem is.

Also, I don't know if this will help but I'll put the code I'm running below.

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.
    }

}

So I did some recent changes, and I took the result function out of the start function and now it starts up, but now when I press start on the timer I need to continuously press the start button for it to update. So how can I make it so that it only starts ticking when the button is pressed and it continues to update to the next number every second.

ndmeiri
  • 4,979
  • 12
  • 37
  • 45

1 Answers1

0

In your ViewController class, your viedDidLoad() method should be changed to the following (remove Selector, see this Stack Overflow answer for more information):

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

Then, move the declaration of result() out of your @IBAction called start. That is, declare it inside of ViewController like so:

import UIKit
class ViewController: UIViewController {

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

    // ... other functions here

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

In the future, whenever you see an error of the form

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

you should first check to make sure that someObject declares a function called someFunction. That is almost always the problem.


In response to your edit, you should implement result() like this:

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

This will cause the text in your UILabel to be update every time the timer fires.

Community
  • 1
  • 1
ndmeiri
  • 4,979
  • 12
  • 37
  • 45