【问题标题】:XCode7/Swift Unrecognized Selector Sent to Instance?XCode7/Swift 无法识别的选择器发送到实例?
【发布时间】:2015-07-16 18:45:52
【问题描述】:

我正在编写一个应用程序,它显示带有弹出框的谢尔宾斯基三角形,允许用户控制在三角形中绘制的三角形的颜色和数量。当尝试通过 UIButton 更改颜色时,我的应用程序崩溃并且我收到错误“无法识别的选择器已发送到实例”。我的应用程序设置为它有一个绘制三角形的 SierpinskiView、一个充当视图委托且主要只是嵌入滚动视图中的 SierpinskiViewController,以及一个控制弹出框并充当委托的 ControlsViewController对于 SierpinskiViewController,并告诉它要告诉 SierpinskiView 绘制多少个三角形以及使用什么颜色。我怀疑我的崩溃可能与委派的错误使用有关。

下面是一些相关代码:

Xcode 将此作为导致应用程序崩溃的方法:

 @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)
        }
    }
}

主色:

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

背景颜色:

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

非常感谢您的帮助!

编辑:这是堆栈跟踪:

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

【问题讨论】:

  • sierpinskiViewController 中的 updateUI 函数在 SierpinskiView 上调用 setNeedsDisplay() 顺便说一句
  • 你能发布实际的堆栈跟踪吗?
  • 请显示完整的错误信息。它包含无法识别哪个选择器以及它已发送到哪个实例的信息。
  • 我在编辑中添加了它

标签: ios swift debugging delegation fractals


【解决方案1】:

你调用了一些函数changeBackgroundColor()。检查你是否在代码中有它。我只看到changeColor()

【讨论】:

  • 我的代码中没有。我在哪里调用了该函数?
  • nvm 我明白了。我在我的代码中调用了一个旧方法!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多