如何debugging“无法识别的select器发送到实例”错误

我正在为我的表视图创build一个自定义表格单元格视图。 将自定义单元格(在Storyboard中)的图像视图连接到我的代码后,我得到以下错误。

[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 ) 

你能告诉我如何解决这个错误?

谢谢。

我在我的项目中添加一个exception断点。

这是它打破的路线。

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

但是我不在代码中使用“图像”。

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

在这里输入图像描述

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

 [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或其他地方configuration了错误的类时,就会发生这种情况。

您可以使用Exception Breakpoints轻松跟踪这些崩溃。

打开Breakpoint Navigator并添加

在这里输入图像描述

一旦添加了exception断点,选项将打开以selectException

在这里输入图像描述

selectObjective-C

运行代码并崩溃应用程序,断点将使您停止代码崩溃的地步。

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

要诊断这个问题,运行“僵尸”检测器的分析器。

按照组指令添加exception断点。 当运行应用程序时,应该停止在有问题的代码行上。

你正在UITableViewCellContentView调用图像..当然不是你打算做什么

我有同样的问题,这对我工作:

覆盖SKScene中的isEqual:

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

在这些情况下,我发现看两件事情很有帮助

  1. 调用堆栈
  2. 每个堆栈帧的局部variables(及其types)

当我有这个错误,通常是因为我发送一条消息到typesA的实例,当我期待typesB.

在这个特定的情况下,你可能不满足父类的要求(因为你的ItemTableViewCell的实例最有可能inheritance)。

你可以向我们展示你的ItemTableViewCell类的代码吗?

您可以使用下面的代码来声明variables:

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

你必须通过indexPath来声明tableview单元格的对象。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) return cell } 

你可以使用Introspection来找出对象是否响应某个特定的select器。

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

只有确实代码能够正常工作,否则肯定会崩溃