Search code examples
iosswiftdebuggingdelegationfractals

XCode7/Swift Unrecognized Selector Sent to Instance?


I am coding an app that displays the Sierpinski triangle with a popover that allows the user to control the colors and number of triangles drawn in the triangle. When trying to change the color via UIButton, my app crashes and I get the error "unrecognized selector sent to instance". My app is set up so that it has a SierpinskiView, which draws the triangles, a SierpinskiViewController, which acts as a delegate for the view and mainly just embeds in a scroll view, and a ControlsViewController, which controls the popover and acts as a delegate for the SierpinskiViewController, and tells it how many triangles to tell the SierpinskiView to draw and in what colors. I suspect my crash may have to do with incorrect usage of delegation.

Here is some relevant code:

Xcode cites this as the method that crashes the app:

 @IBAction func changeColor(sender: UIButton) {
    let color = sender.currentTitle!
    switch mainColorSelected {
    case true:
        switch color {
        case "R": mainColor = UIColor.redColor()
        case "O": mainColor = UIColor.orangeColor()
        case "Y": mainColor = UIColor.yellowColor()
        case "G": mainColor = UIColor.greenColor()
        case "B": mainColor = UIColor.blueColor()
        case "P": mainColor = UIColor.purpleColor()
        case "Bl": mainColor = UIColor.blackColor()
        case "W": mainColor = UIColor.whiteColor()
        default: break
        }
    case false:
        switch color {
        case "R": backgroundColor = UIColor.redColor()
        case "O": backgroundColor = UIColor.orangeColor()
        case "Y": backgroundColor = UIColor.yellowColor()
        case "G": backgroundColor = UIColor.greenColor()
        case "B": backgroundColor = UIColor.blueColor()
        case "P": backgroundColor = UIColor.purpleColor()
        case "Bl": backgroundColor = UIColor.blackColor()
        case "W": backgroundColor = UIColor.whiteColor()
        default: break
        }
    }
}

mainColorSelected:

  var mainColorSelected = false {
    didSet {
        switch mainColorSelected {
        case true: MainColorButton.setTitleColor(UIColor.redColor(), forState: .Normal)
            BackgroundColorButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
        case false: BackgroundColorButton.setTitleColor(UIColor.redColor(), forState: .Normal)
            MainColorButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
        }
    }
}

mainColor:

var mainColor : UIColor = UIColor.whiteColor() {
    didSet {
        updateUI(sierpinskiViewController!)
    }
}

backgroundColor:

 var backgroundColor: UIColor = UIColor.blackColor() {
    didSet {
        updateUI(sierpinskiViewController!)
    }
}

Thanks a ton for any help!

Edit: Here is the stack trace:

2015-07-16 11:51:04.059 Sierpinski Triangle[67645:3302773] - [Sierpinski_Triangle.ControlsViewController changeBackgroundColor:]: unrecognized selector sent to instance 0x7fedbb72c350
2015-07-16 11:51:04.072 Sierpinski Triangle[67645:3302773] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Sierpinski_Triangle.ControlsViewController changeBackgroundColor:]: unrecognized selector sent to instance 0x7fedbb72c350'
*** First throw call stack:
(
0   CoreFoundation                      0x00000001077c8885 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000109571df1 objc_exception_throw + 48
2   CoreFoundation                      0x00000001077d0b5d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x000000010771de3a ___forwarding___ + 970
4   CoreFoundation                      0x000000010771d9e8 _CF_forwarding_prep_0 + 120
5   UIKit                               0x00000001081fe257 -[UIApplication sendAction:to:from:forEvent:] + 125
6   UIKit                               0x00000001081fe1b2 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 79
7   UIKit                               0x0000000108359422 -[UIControl sendAction:to:forEvent:] + 67
8   UIKit                               0x00000001083596c6 -[UIControl _sendActionsForEvents:withEvent:] + 272
9   UIKit                               0x00000001083588a9 -[UIControl touchesEnded:withEvent:] + 599
10  UIKit                               0x0000000108264be8 -[UIWindow _sendTouchesForEvent:] + 835
11  UIKit                               0x00000001082657d6 -[UIWindow sendEvent:] + 865
12  UIKit                               0x000000010821a705 -[UIApplication sendEvent:] + 263
13  UIKit                               0x00000001081f75df _UIApplicationHandleEventQueue + 6031
14  CoreFoundation                      0x00000001076f30f1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15  CoreFoundation                      0x00000001076e8eac __CFRunLoopDoSources0 + 556
16  CoreFoundation                      0x00000001076e8363 __CFRunLoopRun + 867
17  CoreFoundation                      0x00000001076e7d78 CFRunLoopRunSpecific + 488
18  GraphicsServices                    0x000000010c98abca GraphicsServices + 52170
19  UIKit                               0x00000001081fc79b UIApplicationMain + 171
20  Sierpinski Triangle                 0x00000001075c6fbd main + 109
21  libdyld.dylib                       0x0000000109f4aa05 libdyld.dylib + 10757
22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Solution

  • You call some func changeBackgroundColor(). Check if you have it in code. I see only changeColor().