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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25853947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:27:42  来源:igfitidea点击:

How can I debug 'unrecognized selector sent to instance' error

iosswiftuitableview

提问by n179911

I am creating a custom table cell view for my table view. After I connect an image view of custom cell (in storyboard) to my code in swift, I get the following error.

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

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

Can you please tell me how to resolve this error?

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

Thank you.

谢谢你。

I add an exception breakpoint in my project.

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

This is the line where it breaks.

这是它断线的地方。

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

But I don't use 'image' in my code.

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

回答by dbart

Try setting a symbolic breakpoint on -[NSObject(NSObject) doesNotRecognizeSelector:]. Just click [+]in the bottom left corner of the Breakpoint Navigator to add a breakpoint. Then click 'Add Symbolic Breakpoint'. Reproducing your crash now should give you a better idea where in your code the issue occurs.

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

enter image description here

在此处输入图片说明

回答by Vatsal K

You can easily track such crashes using Exception Breakpoints.

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

Open the Breakpoint Navigatorand add the

打开Breakpoint Navigator并添加

enter image description here

在此处输入图片说明

Once you add the Exception Breakpoint, option will be open to choose the Exception.

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

enter image description here

在此处输入图片说明

Select Objective-C.

选择Objective-C

Run the code and crash the application, breakpoint will stop you to the point where the code is crashing.

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

回答by Hot Licks

The critical first step is to analyze the error message:

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

[UITableViewCellContentView image]: unrecognized selector sent to instance

This tells you that the "message" imagewas "sent" to an object of class UITableViewCellContentView. (In other words, an attempt was made to call the method imageon an object of class UITableViewCellContentView.)

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

The first thing to ask is "Does this make any sense at all?" It may be that the named class has an Imagemethod, but not an imagemethod, and so the wrong method name was used on the call. Or it may be that the named method is someMethod:someParm:, but the class implements someMethod:someParm:anotherParm:, meaning that a parameter was omitted on the call.

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

Most often, though, the named class does not have any method even vaguely resembling the named method, meaning that somehow a pointer to the wrong object was used in the failing call.

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

For instance, one might do:

例如,一个人可能会这样做:

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

And get an error along the lines of:

并得到以下方面的错误:

[__NSDictionaryI objectAtIndex:] unrecognized selector sent to instance

because the object retrieved from myDictionarywas, in fact, an NSDictionary, not the NSArray that was expected.

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

Most confusing, unfortunately, is when this sort of error occurs deep in UI system code rather than in your own code. This can happen when you somehow passed the wrong object to a system interface, or perhaps configured the wrong class in Interface Builder or wherever.

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

回答by Bryan

Another possible reason is that the original object was destroyed and then another object was allocated at the same memory address. Then your code sends the message, thinking it still has a pointer to the old object, and Objective-C throws an exception because the new object doesn't understand that message.

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

To diagnose this problem, run the Profiler with 'Zombies' detection.

要诊断此问题,请运行带有“僵尸”检测功能的 Profiler。

回答by Saranjith

Swift 5.0

斯威夫特 5.0

You can use Introspectionto find out whether object responds to a particular selector or not..

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

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

Only if it is true that code will work.. otherwise it will crash for sure

只有当代码确实可以工作时..否则它肯定会崩溃

回答by ABC

In these scenarios I've found it helpful to look at two things

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

  1. The call-stack
  2. The local variables (and their types) at each stack frame
  1. 调用栈
  2. 每个堆栈帧的局部变量(及其类型)

When I've had this error, it's usually because I sent a message to an instance of Type A, when I was expecting Type B.

当我遇到这个错误时,通常是因为我向类型 A 的实例发送了一条消息,而我期待的是类型 B。

In this specific scenario, you may not be satisfying a requirement of a parent class (given your instance of ItemTableViewCell most likely inherits).

在此特定场景中,您可能无法满足父类的要求(考虑到您的 ItemTableViewCell 实例很可能继承)。

Can you maybe show us the code for your ItemTableViewCell class?

您能否向我们展示您的 ItemTableViewCell 类的代码?

回答by Yogesh shelke

You can use the following code to declare the variable:

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

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

回答by Rohit Parsana

You have to pass indexPathto declare object of tableview cell.

您必须通过indexPath以声明 tableview 单元格的对象。

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

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

    return cell
}

回答by Razvan

I had the same problem, and this worked for me:

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

Override the isEqual in your SKScene:

覆盖 SKScene 中的 isEqual:

- (BOOL)isEqual:(id)other {

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