-1

Thanks for answering my question.

I'm writing my first app in Swift. It was running fine until I added a UISwitch and programmed it. It gives me this output when I attempt to toggle the switch:

2015-08-02 14:02:36.091 Hello World[1885:633995] HangTracer interval is 0, forcing to 1s
2015-08-02 14:02:38.890 Hello World[1885:633995] -[Hello_World.ViewController myButton:]: unrecognized selector sent to instance 0x144e3a880
2015-08-02 14:02:38.891 Hello World[1885:633995] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Hello_World.ViewController myButton:]: unrecognized selector sent to instance 0x144e3a880'
*** First throw call stack:
(0x185ccc4d0 0x19a0aff9c 0x185cd31ec 0x185cd0188 0x185bd520c 0x18b36de08 0x18b36dd84 0x18b356934 0x18b5bef6c 0x18b7bd724 0x18b87858c 0x18b4d5b50 0x18b369104 0x18b877c30 0x18b32928c 0x18b3273ac 0x18b367088 0x18b3665ec 0x18b337908 0x18b335fac 0x185c83d6c 0x185c83800 0x185c81500 0x185bb1280 0x190b600cc 0x18b39edf8 0x10008614c 0x19a8f28b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

I have no idea what's wrong. Why is myButton affecting a different, unrelated variable? (mySwitch). If need be I can upload the code as well.

  • 1
    Add a breakpoint on All exceptions and try doing the same thing again. It should break exactly where the crash occurs and you can see what's the state of the app using the debugger console – marosoaie Aug 02 '15 at 21:09
  • Provide more details, at least myButton function implementation and where you call it. – s1ddok Aug 02 '15 at 21:09
  • the entire code is available at https://github.com/Ph0enix0/helloworldswift/blob/master/ViewController.swift – Ryan Broman Aug 02 '15 at 21:18
  • I also added breakpoints to all exceptions and it broke on: class AppDelegate: UIResponder, UIApplicationDelegate { in the AppDelegate file – Ryan Broman Aug 02 '15 at 21:23
  • possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Aug 02 '15 at 22:05
  • @HotLicks it doesnt seem like it. that person's code breaks at a different point of mine, it just uses the same output. – Ryan Broman Aug 02 '15 at 22:11
  • Read the title and study the answers. It's not about the specific problem. – Hot Licks Aug 03 '15 at 01:55

2 Answers2

1

You have to change this line:

mySwitch.addTarget(self, action: Selector("switchIsChanged"), forControlEvents: UIControlEvents.ValueChanged)

To this:

mySwitch.addTarget(self, action: Selector("switchIsChanged:"), forControlEvents: UIControlEvents.ValueChanged)

You can skip the ":" if the selector takes no arguments but if the selector takes arguments you have to add the ":".

Darko
  • 9,655
  • 9
  • 36
  • 48
  • I still get the SIGABRT error on the same breakpoint, if that helps at all? – Ryan Broman Aug 02 '15 at 21:56
  • I just tried your code in Xcode and it works without problems. I assume you have set an connection in Interface Builder and later on renamed or removed the method in the Swift file. The error message you have pasted here refers to "[Hello_World.ViewController myButton:]". But there is no "myButton" Selector in your github link. – Darko Aug 02 '15 at 22:20
  • Main.storyboard is fine. myButton is correctly assigned and I never made a selector. – Ryan Broman Aug 02 '15 at 22:38
0

All I did to fix the issue was copy and paste ViewController.swift into a new project and recreate Main.storyboard. No idea how it worked but @Darko got it right. Thanks Darko!