-[NSObject(NSObject) doesNotRecognizeSelector:] 调用时崩溃 -[ViewController prepareForSegue:sender:]

Posted

技术标签:

【中文标题】-[NSObject(NSObject) doesNotRecognizeSelector:] 调用时崩溃 -[ViewController prepareForSegue:sender:]【英文标题】:-[NSObject(NSObject) doesNotRecognizeSelector:] crash when call -[ViewController prepareForSegue:sender:] 【发布时间】:2013-08-19 04:54:00 【问题描述】:

我正在使用 Crashlytics 来检测我的应用程序中的崩溃。我偶尔会收到以下崩溃报告。

关键的崩溃点是 -[NSObject(NSObject) doesNotRecognizeSelector:] 和 MyViewController.m 第 596 行 -[MyViewController prepareForSegue:sender:]。我从日志中不知道这是什么问题。是否可能是多线程问题?我还使用 Parse SDK 从 Parse 云中检索数据。并且 prepareForSegue:sender: 方法将在用户点击公开指示器时被调用,它应该在主线程中运行。请你给我一些提示来解决这个问题。提前致谢。

这就是我如何调用 prepareForSegue:sender: 方法。

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

    [self performSegueWithIdentifier:@"oneSegue" sender:indexPath];


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if ([segue.identifier isEqualToString:@"oneSegue"]) 
        NSIndexPath *indexPath = (NSIndexPath*)sender;
        OneViewController *destViewController = segue.destinationViewController;
        OneClass* oneObject = self.array[indexPath.row];
        destViewController.objectName = oneObject.name;
    

prepareForSegue:sender: 方法由accessoryButtonTappedForRowWithIndexPath: 方法触发。我看不到任何其他调用 prepareForSegue:sender: 方法的方法。所以我不知道为什么 indexPath.row 会导致异常。

Fatal Exception NSInvalidArgumentException
-[UITableViewCell row]: unrecognized selector sent to instance 0x1f8a6dd0
0    CoreFoundation  __exceptionPreprocess + 162
1    libobjc.A.dylib     objc_exception_throw + 30
2    **CoreFoundation    -[NSObject(NSObject) doesNotRecognizeSelector:] + 170**
3    CoreFoundation  ___forwarding___ + 392
4    CoreFoundation  _CF_forwarding_prep_0 + 24
5    MyApp  
MyViewController.m line 596
**-[MyViewController prepareForSegue:sender:]**
6    UIKit   -[UIStoryboardSegueTemplate _perform:] + 128
7    UIKit   -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 828
8    UIKit   -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 156
9 ...    Foundation  __NSFireDelayedPerform + 450
10   CoreFoundation  __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
11   CoreFoundation  __CFRunLoopDoTimer + 272
12   CoreFoundation  __CFRunLoopRun + 1232
13   CoreFoundation  CFRunLoopRunSpecific + 356
14   CoreFoundation  CFRunLoopRunInMode + 104
15   GraphicsServices    GSEventRunModal + 74
16   UIKit   UIApplicationMain + 1120
17   MyApp  
main.m line 16
main
18   libdyld.dylib   start
9 Threads
com.apple.main-thread Crashed
0 ...    libsystem_kernel.dylib  __pthread_kill + 8
1    libsystem_c.dylib   pthread_kill + 58
2    libsystem_c.dylib   abort + 94
3    libc++abi.dylib     abort_message + 74
4    libc++abi.dylib     default_terminate() + 24
5    libobjc.A.dylib     _objc_terminate() + 146
6    libc++abi.dylib     safe_handler_caller(void (*)()) + 78
7    libc++abi.dylib     std::terminate() + 19
8    libc++abi.dylib     __cxa_current_exception_type
9    libobjc.A.dylib     objc_exception_rethrow + 12
10   CoreFoundation  CFRunLoopRunSpecific + 456
11   CoreFoundation  CFRunLoopRunInMode + 104
12   GraphicsServices    GSEventRunModal + 74
13   UIKit   UIApplicationMain + 1120
14   MyApp  
main.m line 16
main
com.apple.libdispatch-manager
0 ...    libsystem_kernel.dylib  kevent64 + 24
1    libdispatch.dylib   _dispatch_mgr_invoke + 796
2    libdispatch.dylib   _dispatch_mgr_thread$VARIANT$mp + 35
WebThread
0 ...    libsystem_kernel.dylib  mach_msg_trap + 20
1    libsystem_kernel.dylib  mach_msg + 40
2    CoreFoundation  __CFRunLoopServiceMachPort + 128
3    CoreFoundation  __CFRunLoopRun + 882
4    CoreFoundation  CFRunLoopRunSpecific + 356
5    CoreFoundation  CFRunLoopRunInMode + 104
6    WebCore     RunWebThread(void*) + 444
7    libsystem_c.dylib   _pthread_start + 308
com.crashlytics.MachExceptionServer
0    libsystem_kernel.dylib  mach_msg_trap + 20
1    libsystem_kernel.dylib  mach_msg + 40
2    MyApp   CLSMachExceptionServer
3    libsystem_c.dylib   _pthread_start + 308
com.apple.NSURLConnectionLoader
0 ...    libsystem_kernel.dylib  mach_msg_trap + 20
1    libsystem_kernel.dylib  mach_msg + 40
2    CoreFoundation  __CFRunLoopServiceMachPort + 128
3    CoreFoundation  __CFRunLoopRun + 882
4    CoreFoundation  CFRunLoopRunSpecific + 356
5    CoreFoundation  CFRunLoopRunInMode + 104
6    Foundation  +[NSURLConnection(Loader) _resourceLoadLoop:] + 308
7    Foundation  __NSThread__main__ + 972
8    libsystem_c.dylib   _pthread_start + 308
Thread 5
0 ...    libsystem_kernel.dylib  mach_msg_trap + 20
1    libsystem_kernel.dylib  mach_msg + 40
2    CoreFoundation  __CFRunLoopServiceMachPort + 128
3    CoreFoundation  __CFRunLoopRun + 882
4    CoreFoundation  CFRunLoopRunSpecific + 356
5    CoreFoundation  CFRunLoopRunInMode + 104
6    Foundation  -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 254
7    Foundation  -[NSRunLoop(NSRunLoop) run] + 80
8    MyApp  
PF_AFURLConnectionOperation.m line 151
+[PF_AFURLConnectionOperation networkRequestThreadEntryPoint:]
9    Foundation  __NSThread__main__ + 972
10   libsystem_c.dylib   _pthread_start + 308
Thread 6
0 ...    libsystem_kernel.dylib  __psynch_cvwait + 24
1    libsystem_c.dylib   _pthread_cond_wait + 646
2    libsystem_c.dylib   pthread_cond_wait + 40
3    Foundation  -[NSCondition wait] + 194
4    MyApp  
PFCommandCache.m line 495
-[PFCommandCache runLoop]
5    Foundation  __NSThread__main__ + 972
6    libsystem_c.dylib   _pthread_start + 308
com.apple.CFSocket.private
0 ...    libsystem_kernel.dylib  __select + 20
1    CoreFoundation  __CFSocketManager + 678
2    libsystem_c.dylib   _pthread_start + 308
Thread 8
0 ...    libsystem_kernel.dylib  __workq_kernreturn + 8
1    libsystem_c.dylib   _pthread_workq_return + 18
2    libsystem_c.dylib   _pthread_wqthread + 365
Blog Privacy

【问题讨论】:

显示你在方法'-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'中编写的代码 我尝试安装 crashlytics,但我认为这是一个早期阶段,您只能通过邀请安装应用程序? @abbood,你现在可以免费使用了。 【参考方案1】:

这是因为在您打开的视图中,您试图在 UITableViewCell 上调用名为 row 的方法。

您可以在崩溃日志的第一行看到这一点:

-[UITableViewCell row]: unrecognized selector sent to instance 0x1f8a6dd0

您应该追踪您在哪个对象上调用row,并确保您在您认为自己是的对象上调用它,而不是UITableViewCell

【讨论】:

Jsdodgers 是对的......检查你的 prepareForSegue 方法以调用 row 。可能是因为您希望 sender 会响应 row,但结果却是 UITableViewCell 查看您的 prepareForSegue,您将 sender 转换为 NSIndexPath,而它实际上是 UITableViewCell。然后您正在调用 indexPath.row,这就是您收到此错误的原因。 @Jsdodgers ,感谢您的回复,我实际上将 (NSIndexPath *)indexPath 传递给了发件人,正如您在 [self performSegueWithIdentifier:@"oneSegue" sender:indexPath]; 中看到的那样必须在其他地方调用 performSegueWithIdentifier:sender: 方法并传递 UITableViewCell 的对象。但我找不到其他任何地方显式调用此方法。我认为我的解决方法是检查发件人类型是否为 NSIndexPath,如果不是则忽略它。 您将 indexPath 传递给 performSegueWithIdentifier,而不是 prepareForSegue,我相信它会在按下单元格并且发送者是您按下的单元格时自动调用(尽管我不使用情节提要,所以我可能对此不正确)。 @Jsdodgers 谢谢,我会先用 isKindOfClass 保护我的代码。

以上是关于-[NSObject(NSObject) doesNotRecognizeSelector:] 调用时崩溃 -[ViewController prepareForSegue:sender:]的主要内容,如果未能解决你的问题,请参考以下文章

id, NSObject *, id<NSObject>区别

IOS学习基础NSObject.h学习

NSObject头文件解析 / 消息机制 / Runtime解读

从 NSObject 类更新 UITabBarController 栏项目

ios开发之 NSObject详解

从 NSObject 类创建一个数组