3
votes

Edit 3:

For other people getting this error, this is what happened here.

I originally created the IBAction for the slider as "numberOfSounds". I then created a class property called "numberOfSounds" and renamed the IBAction to "sliderValueChanged".

I expected the connection to automatically update the connection, BUT IT DOESN'T. So, since I dumbly used the same name for the variable, I missed the connection. See @rob accepted answer below for the telltale clue.

The solution was to delete the previous connection and control-drag from the slider in the storyboard to the sliderValueChanged IBAction in the ViewController.

Original question:

I'm getting this extremely common error and like many others, I'm having a hard time understanding the fix. Manipulating the slider causes a crash, which is this message:

2015-10-17 13:35:59.765 Test App[25969:2985698] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test_App.ViewController numberOfSounds:]: unrecognized selector sent to instance 0x7fc439e26740'

The breakpoint is on "numberOfSounds = Int(sender.value)".

import UIKit
import AVFoundation

class ViewController: UIViewController {
  var numberOfSounds: Int = 0

  @IBAction func sliderValueChanged(sender: UISlider) {
    numberOfSounds = Int(sender.value)
  }
}

I think I'm understanding something of the problem, being that despite numberOfSounds being a property of ViewController, its not available in the scope of sliderValueChanged.

I've read through the top 15-20 google searches for "unrecognized selector sent to instance" but still haven't found something that seems to answer the problem.

Thanks for the help.

Edit: (Adding more code)

class ViewController: UIViewController {
  var numberOfSounds: Int = 0

  @IBAction func sliderValueChanged(sender: UISlider) {
    numberOfSounds = Int(sender.value)
  }
  @IBOutlet weak var numberOfSoundsLabel: UILabel!
  @IBAction func PlaySound(sender: UIButton) {}

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }

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

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

Edit 2: (Adding full stack trace)

2015-10-17 13:59:24.303 Test App[26230:3006546] -[Test_App.ViewController numberOfSounds:]: unrecognized selector sent to instance 0x7fedea730350 2015-10-17 13:59:24.313 Test App[26230:3006546] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test_App.ViewController numberOfSounds:]: unrecognized selector sent to instance 0x7fedea730350' * First throw call stack: ( 0 CoreFoundation 0x000000010a7d7f65 exceptionPreprocess + 165 1 libobjc.A.dylib
0x000000010c469deb objc_exception_throw + 48 2 CoreFoundation
0x000000010a7e058d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010a72df7a ___forwarding_
+ 970 4 CoreFoundation 0x000000010a72db28 _CF_forwarding_prep_0 + 120 5 UIKit
0x000000010aff41fa -[UIApplication sendAction:to:from:forEvent:] + 92 6 UIKit 0x000000010b158504 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x000000010b1587d0 -[UIControl _sendActionsForEvents:withEvent:] + 311 8 UIKit 0x000000010b244770 -[UISlider beginTrackingWithTouch:withEvent:] + 1083 9 UIKit
0x000000010b15739d -[UIControl touchesBegan:withEvent:] + 136 10 UIKit 0x000000010b05e894 -[UIWindow _sendTouchesForEvent:] + 308 11 UIKit 0x000000010b05f691 -[UIWindow sendEvent:] + 865 12 UIKit
0x000000010b011752 -[UIApplication sendEvent:] + 263 13 UIKit
0x000000010afecfcc _UIApplicationHandleEventQueue + 6693 14 CoreFoundation 0x000000010a7040a1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 15 CoreFoundation 0x000000010a6f9fcc __CFRunLoopDoSources0 + 556 16 CoreFoundation 0x000000010a6f9483 __CFRunLoopRun + 867 17 CoreFoundation
0x000000010a6f8e98 CFRunLoopRunSpecific + 488 18 GraphicsServices
0x000000010ed6cad2 GSEventRunModal + 161 19 UIKit
0x000000010aff2676 UIApplicationMain + 171 20 Test App
0x000000010a5fc1ad main + 109 21 libdyld.dylib
0x000000010cfb092d start + 1 22 ???
0x0000000000000001 0x0 + 1 )

1
Are you sure that code exactly causes the issue and that is the exact code in your Xcode, just copied it (not retyped it?) because copying it in playground and calling the function works without any problem - luk2302
@luk2302 It should. Its a subset of more unrelated code. Umm, let me test it real quick. - Geuis
pastebin.com/Di7pYwmN for example just works :/ as it should, there is not yet anything that could go wrong ^^ - luk2302
Clear any breakpoint and check the stack backtrack when you get the exception. That code as shown works - Paulw11
@Paulw11 That's not what the error message is saying. A control in the storyboard is wired to an action named "numberOfSounds:", which is not implemented in "ViewController". It's an action, not a property getter. - Darren

1 Answers

5
votes

The telltale issue is the presence of the colon in -[Test_App.ViewController numberOfSounds:]. That means that you have something that is trying to call a method called numberOfSounds: with one parameter.

Assuming you don't have anything in your code that would be doing that, I would double check the outlets/actions for the slider in the "Connections Inspector" IB and make sure you don't have an action like the following:

enter image description here

That would result in the error message you describe. If you have any extraneous actions like the above, just click the little "x" to remove them.

--

This can happen if you once attempted to connect an action from the slider to your view controller, calling the action numberOfSounds, but then realized the mistake upon seeing the IBAction code and proceeded to delete/edit the IBAction function in the code, but neglecting to adjust the connection in IB, too. That's a common workflow that would result in this sort of behavior.