1

I'm newbie in Swift and I'm currenlty trying to learn it but I faced a mystery issue. As you can see in the image below, there are some controls that are linked properly to this View Controller and upon pressing a button in the UI we execute this code below but I always got an EXC_BAD_INSTRUCTION. I don't understand why!!

enter image description here

Here is some info from the stack trace.

(lldb) thread info
thread #1: tid = 0x9319, 0x0006c5d5 App`App.AViewController.sendCommentViaEmail (self=<unavailable>)() -> () + 9349 at AViewController.swift:49, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

(lldb) frame select 1
frame #1: 0x037067cd libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84
libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84:
-> 0x37067cd:  addl   $0x1c, %esp
   0x37067d0:  popl   %esi
   0x37067d1:  popl   %edi
   0x37067d2:  popl   %ebx

EDITED:

Stacktrace below

* thread #1: tid = 0x1e19, 0x0005a5d5 A`A.AViewController.sendCommentViaEmail (self=<unavailable>)() -> () + 9349 at AViewController.swift:49, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
  * frame #0: 0x0005a5d5 A`A.AViewController.sendCommentViaEmail (self=<unavailable>)() -> () + 9349 at AViewController.swift:49
    frame #1: 0x036f47cd libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84
    frame #2: 0x01ea323d UIKit`-[UIApplication sendAction:to:from:forEvent:] + 99
    frame #3: 0x02213840 UIKit`-[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
    frame #4: 0x036f47cd libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84
    frame #5: 0x01ea323d UIKit`-[UIApplication sendAction:to:from:forEvent:] + 99
    frame #6: 0x01ea31cf UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    frame #7: 0x01fd6e86 UIKit`-[UIControl sendAction:to:forEvent:] + 69
    frame #8: 0x01fd72a3 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 598
    frame #9: 0x01fd650d UIKit`-[UIControl touchesEnded:withEvent:] + 660
    frame #10: 0x01ef360a UIKit`-[UIWindow _sendTouchesForEvent:] + 874
    frame #11: 0x01ef40e5 UIKit`-[UIWindow sendEvent:] + 791
    frame #12: 0x01eb9549 UIKit`-[UIApplication sendEvent:] + 242
    frame #13: 0x01ec937e UIKit`_UIApplicationHandleEventFromQueueEvent + 20690
    frame #14: 0x01e9db19 UIKit`_UIApplicationHandleEventQueue + 2206
    frame #15: 0x039791df CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    frame #16: 0x0396eced CoreFoundation`__CFRunLoopDoSources0 + 253
    frame #17: 0x0396e248 CoreFoundation`__CFRunLoopRun + 952
    frame #18: 0x0396dbcb CoreFoundation`CFRunLoopRunSpecific + 443
    frame #19: 0x0396d9fb CoreFoundation`CFRunLoopRunInMode + 123
    frame #20: 0x085dc24f GraphicsServices`GSEventRunModal + 192
    frame #21: 0x085dc08c GraphicsServices`GSEventRun + 104
    frame #22: 0x01ea18b6 UIKit`UIApplicationMain + 1526
    frame #23: 0x000919cc A`main(argc=1, argv=0xbffaa62c) + 76 at main.m:12
    frame #24: 0x04490ac9 libdyld.dylib`start + 1

Here are the properties I'm using: I got the same result whether they are linked to the UI or not.

Properties linked to the UI controls and same result

Maystro
  • 2,907
  • 8
  • 36
  • 71

3 Answers3

2

Where does emailToMyselfSwitch come from? If it's an IBOutlet it seems that you forgot to connect it from IB to your code. If that's not the case please show more context.

fz.
  • 3,233
  • 22
  • 20
  • Nope, it's already connected and even it's not, it should not crash!! At least this what I know.. – Maystro Feb 08 '15 at 18:52
  • 2
    It will crash. Because you are defining the property as an unwrapped optional but without the connection from Interface Builder, the property will be nil: therefore trying to access your unwrapped (nil) optional will try to dereference a null pointer – fz. Feb 08 '15 at 18:58
  • Make sure the control is linked to your IBOutlet by right clicking the switch control on your interface builder – fz. Feb 08 '15 at 18:59
  • 3
    It still seems to me that the switch control is not connected to your outlet. Maybe the *action* is connected to the IBAction but not the IBOutlet and you need to connect both. – fz. Feb 08 '15 at 19:20
1

This error EXC_BAD_INSTRUCTION generally means that you are calling a property or method (selector?) on a null object or an object that can't respond to the selector ie self.null.on or a property that cannot be accessed because it was released.

I've had trouble in the past with these weak vars falling away. Try deleting the weak in your property.

Aaron
  • 1,390
  • 1
  • 16
  • 30
1

your code is correct. The problem is not in the connection with outlet. I´ve tested and prints: "Value: nil" The problem is in other part of code. The message "self=" is not normal. You can show us more information.

enter image description here

Miguel
  • 957
  • 10
  • 12