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

n17*_*911 98 uitableview ios swift

我正在为表视图创建自定义表格单元格视图.在我将自定义单元格(在故事板中)的图像视图连接到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
)
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

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

这是它破裂的路线.

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

但我不在我的代码中使用'image'.

dba*_*art 100

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

在此输入图像描述

  • 也可以添加[异常断点](http://stackoverflow.com/a/17802723/1145804). (17认同)

Vat*_*l K 33

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

打开Breakpoint Navigator并添加

在此输入图像描述

添加异常断点后,将打开选项以选择Exception.

在此输入图像描述

选择Objective-C.

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


Hot*_*cks 28

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

[UITableViewCellContentView image]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)

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

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

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

例如,有人可能会:

NSArray* myArray = [myDictionary objectForKey:@"values"];
NSString* myString = [myArray objectAtIndex:5];
Run Code Online (Sandbox Code Playgroud)

并得到一个错误:

[__NSDictionaryI objectAtIndex:] unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)

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

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


Bry*_*yan 6

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

要诊断此问题,请使用"Zombies"检测运行Profiler.