如何调试发送到实例'的无法识别的选择器?错误

时间:2014-09-15 17:54:59

标签: ios swift uitableview

我正在为表格视图创建自定义表格单元格视图。在我将自定义单元格(在故事板中)的图像视图连接到swift中的代码后,我收到以下错误。

[UITableViewCellContentView image]: unrecognized selector sent to instance 0x7fb4fad7fd20'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ccbb3f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010e7e9bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010ccc250d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010cc1a7fc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010cc1a398 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010d7d8881 -[UITableViewCell _marginWidth] + 151
    6   UIKit                               0x000000010d7ca23d -[UITableViewCell _separatorFrame] + 70
    7   UIKit                               0x000000010d7ca6fa -[UITableViewCell _updateSeparatorContent] + 360
    8   UIKit                               0x000000010d7d4e85 -[UITableViewCell _setSectionLocation:animated:forceBackgroundSetup:] + 1174
    9   UIKit                               0x000000010d634ea8 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1822
    10  UIKit                               0x000000010d5b5eae +[UIView(Animation) performWithoutAnimation:] + 65
    11  UIKit                               0x000000010d63477b -[UITableView _configureCellForDisplay:forIndexPath:] + 312
    12  UIKit                               0x000000010d63bcec -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 533
    13  UIKit                               0x000000010d61b7f1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
    14  UIKit                               0x000000010d63165c -[UITableView layoutSubviews] + 213
    15  UIKit                               0x000000010d5be199 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
    16  QuartzCore                          0x00000001114b6f98 -[CALayer layoutSublayers] + 150
    17  QuartzCore                          0x00000001114abbbe _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    18  QuartzCore                          0x00000001114aba2e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    19  QuartzCore                          0x0000000111419ade _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    20  QuartzCore                          0x000000011141abea _ZN2CA11Transaction6commitEv + 390
    21  QuartzCore                          0x000000011141b255 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    22  CoreFoundation                      0x000000010cbf0347 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    23  CoreFoundation                      0x000000010cbf02a0 __CFRunLoopDoObservers + 368
    24  CoreFoundation                      0x000000010cbe60d3 __CFRunLoopRun + 1123
    25  CoreFoundation                      0x000000010cbe5a06 CFRunLoopRunSpecific + 470
    26  GraphicsServices                    0x0000000110daa9f0 GSEventRunModal + 161
    27  UIKit                               0x000000010d545550 UIApplicationMain + 1282
    28  TestWork                          0x000000010caa432e top_level_code + 78
    29  TestWork                          0x000000010caa436a main + 42
    30  libdyld.dylib                       0x000000010efc3145 start + 1
    31  ???                                 0x0000000000000001 0x0 + 1
)

您能否告诉我如何解决此错误?

谢谢。

我在项目中添加了一个异常断点。

这是它破裂的路线。

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as ItemTableViewCell  <---------------

但我在代码中没有使用'image'。

9 个答案:

答案 0 :(得分:97)

尝试在-[NSObject(NSObject) doesNotRecognizeSelector:]上设置符号断点。只需单击Breakpoint Navigator左下角的[+]即可添加断点。然后单击“添加符号断点”。现在重现崩溃应该可以让您更好地了解代码中出现问题的位置。

enter image description here

答案 1 :(得分:31)

您可以使用Exception Breakpoints轻松跟踪此类崩溃。

打开Breakpoint Navigator并添加

enter image description here

添加例外断点后,将打开选项以选择Exception

enter image description here

选择Objective-C

运行代码并使应用程序崩溃,断点将阻止您到达代码崩溃的程度。

答案 2 :(得分:27)

关键的第一步是分析错误消息:

[UITableViewCellContentView image]: unrecognized selector sent to instance

这告诉您“消息”image被“发送”到类UITableViewCellContentView的对象。 (换句话说,尝试在类UITableViewCellContentView的对象上调用方法image。)

首先要问的是“这根本没有意义吗?”可能是命名类具有Image方法,但不是image方法,因此在调用时使用了错误的方法名称。或者可能是命名方法是someMethod:someParm:,但是类实现了someMethod:someParm:anotherParm:,这意味着在调用时省略了参数。

但是,大多数情况下,命名类没有任何方法,甚至模糊地类似于命名方法,这意味着在失败的调用中以某种方式使用了指向错误对象的指针。

例如,有人可能会这样做:

NSArray* myArray = [myDictionary objectForKey:@"values"];
NSString* myString = [myArray objectAtIndex:5];

并且出现以下错误:

[__NSDictionaryI objectAtIndex:] unrecognized selector sent to instance

因为从myDictionary检索的对象实际上是NSDictionary,而不是预期的NSArray。

遗憾的是,最令人困惑的是,这种错误发生在UI系统代码中,而不是在您自己的代码中。当您以某种方式将错误的对象传递给系统接口,或者可能在Interface Builder或任何地方配置了错误的类时,就会发生这种情况。

答案 3 :(得分:6)

另一个可能的原因是原始对象被破坏,然后在同一个内存地址分配了另一个对象。然后你的代码发送消息,认为它仍然有一个指向旧对象的指针,而Objective-C抛出异常,因为新对象不理解该消息。

要诊断此问题,请使用&#39; Zombies&#39;运行Profiler。检测

答案 4 :(得分:1)

在这些场景中,我发现看两件事很有帮助

  1. call-stack
  2. 每个堆栈框架的局部变量(及其类型)
  3. 当我遇到这个错误时,通常是因为当我期待B型时,我向A类实例发送了一条消息。

    在这种特定情况下,您可能无法满足父类的要求(假设您的ItemTableViewCell实例很可能继承)。

    你能告诉我们你的ItemTableViewCell类的代码吗?

答案 5 :(得分:1)

您可以使用以下代码声明变量:

let noteListTableViewCellobject = "NoteListTableViewCell";` `// Note listTablecell create custom cell`

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

    var cell:NoteListTableViewCell? = tableView.dequeueReusableCellWithIdentifier(noteListTableViewCellobject) as? NoteListTableViewCell

    if (cell == nil) {
        let nib:Array = NSBundle.mainBundle().loadNibNamed("NoteListTableViewCell", owner: self, options: nil)
        cell = nib[0] as? NoteListTableViewCell
    }
}

答案 6 :(得分:0)

我遇到了同样的问题,这对我有用:

覆盖SKScene中的isEqual:

- (BOOL)isEqual:(id)other {

    if (![other isMemberOfClass:[SKScene class]]) {
        return false;
    }
    return [super isEqual:other];
}

答案 7 :(得分:0)

您必须传递indexPath来声明tableview单元格的对象。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    return cell
}

答案 8 :(得分:0)

您可以使用 Introspection 来确定对象是否响应特定选择器。

let canWork = yourObject.respondsToSelector(Selector("image"))   // true

只有当代码确实有效时才会运行..否则肯定会崩溃

相关问题