-[XBMessage messageHash]:发送到实例的无法识别的选择器

Posted

技术标签:

【中文标题】-[XBMessage messageHash]:发送到实例的无法识别的选择器【英文标题】:-[XBMessage messageHash]: unrecognized selector sent to instance 【发布时间】:2015-09-01 11:59:20 【问题描述】:

我已经集成了来自 Cocoapods 的以下库,您可以在 https://github.com/EugeneNguyen/XBChatModule 找到代码及其示例代码

pod 'XBChatModule'

该库用于在 Xcode 项目中集成 XMPP Chat。 我还从它的自述文件中添加了建议的代码。供您参考,我将其粘贴在下面

AppDelegate.m

    [[XBChatModule sharedInstance] setUsername:@"admin"];
    [[XBChatModule sharedInstance] setPassword:@"admin"];
    [[XBChatModule sharedInstance] setHost:@"sflashcard.com"];
    [[XBChatModule sharedInstance] connect];

    [[XBChatModule sharedInstance] setAvatarFormat:@"http://dev.sflashcard.com/images/mantis_logo.png?test=%@"];
    [[XBChatModule sharedInstance] setAvatarPlaceHolder:[UIImage imageNamed:@"girl_9"]];

视图控制器继承自XBMessageViewController

ViewController.m

- (void)viewDidLoad

    self.jidStr = @"binh.nx@sflashcard.com";
    [super viewDidLoad];


- (void)viewWillAppear:(BOOL)animated




- (void)viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:@"XBChatModuleNewAvatar" object:nil];


- (void)viewDidDisappear:(BOOL)animated

    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];


- (void)reloadData

    [self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

在我发送消息之前,代码运行良好。每当我按下发送按钮时,它都会通过在控制台中抛出以下错误来使应用程序崩溃

2015-09-01 17:20:41.742 Test[16064:251944] -[XBMessage messageHash]: unrecognized selector sent to instance 0x7ffc91cc4180
2015-09-01 17:20:41.800 Test[16064:251944] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XBMessage messageHash]: unrecognized selector sent to instance 0x7ffc91cc4180'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000011260aa75 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001122a3bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000112611d1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001125697ef ___forwarding___ + 495
    4   CoreFoundation                      0x0000000112569578 _CF_forwarding_prep_0 + 120
    5   Test                                0x000000010ebaf7ed -[JSQMessagesCollectionViewFlowLayout messageBubbleSizeForItemAtIndexPath:] + 301
    6   Test                                0x000000010ebb02a7 -[JSQMessagesCollectionViewFlowLayout sizeForItemAtIndexPath:] + 71
    7   Test                                0x000000010ebc5c93 -[JSQMessagesViewController collectionView:layout:sizeForItemAtIndexPath:] + 131
    8   UIKit                               0x00000001116369be -[UICollectionViewFlowLayout _getSizingInfos] + 988
    9   UIKit                               0x0000000111637839 -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 526
    10  UIKit                               0x00000001116332b7 -[UICollectionViewFlowLayout collectionViewContentSize] + 66
    11  Test                                0x000000010ebc2d85 -[JSQMessagesViewController scrollToBottomAnimated:] + 325
    12  Test                                0x000000010ebc2c34 -[JSQMessagesViewController finishReceivingMessageAnimated:] + 388
    13  Test                                0x000000010ebc2aa0 -[JSQMessagesViewController finishReceivingMessage] + 48
    14  Test                                0x000000010ebfaac3 -[XBMessageViewController loadDataToTable] + 1507
    15  Test                                0x000000010ebf9be9 -[XBMessageViewController viewDidLoad] + 73
    16  Test                                0x000000010ead0760 -[ViewController viewDidLoad] + 96
    17  UIKit                               0x0000000111104580 -[UIViewController loadViewIfRequired] + 738
    18  UIKit                               0x000000011110477e -[UIViewController view] + 27
    19  UIKit                               0x0000000111023509 -[UIWindow addRootViewControllerViewIfPossible] + 58
    20  UIKit                               0x00000001110238a1 -[UIWindow _setHidden:forced:] + 247
    21  UIKit                               0x000000011102ff8c -[UIWindow makeKeyAndVisible] + 42
    22  UIKit                               0x0000000110fda0c2 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
    23  UIKit                               0x0000000110fdce3e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
    24  UIKit                               0x0000000110fdbd35 -[UIApplication workspaceDidEndTransaction:] + 179
    25  FrontBoardServices                  0x0000000115aab243 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    26  CoreFoundation                      0x000000011253fc7c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    27  CoreFoundation                      0x00000001125359c5 __CFRunLoopDoBlocks + 341
    28  CoreFoundation                      0x0000000112535183 __CFRunLoopRun + 851
    29  CoreFoundation                      0x0000000112534bc6 CFRunLoopRunSpecific + 470
    30  UIKit                               0x0000000110fdb7a2 -[UIApplication _run] + 413
    31  UIKit                               0x0000000110fde580 UIApplicationMain + 1282
    32  Test                                0x000000010ead06d3 main + 115
    33  libdyld.dylib                       0x0000000112b3c145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

据我所知,我没有在 XBMessage 类中实现函数 messageHash ,但相同的代码在我从 Github 下载的示例代码中运行良好。请帮我解决这个错误。

【问题讨论】:

【参考方案1】:

XBChatModule 所基于的库现已更新。如果您查看XBMessage 符合的协议的历史记录:https://github.com/jessesquires/JSQMessagesViewController/commits/develop/JSQMessagesViewController/Model/JSQMessageData.h,您可以看到自编写XBChatModule 以来,方法messageHash 已被要求。

因此,我建议联系XBChatModule 的开发人员并让他们更新他们的代码,或者实现您自己的messageHash 方法。

【讨论】:

感谢@michaelrccurtis,事实上我不知道messageHash 中应该包含什么,尽管我阅读了方法声明中的文档。你能建议我一个这样的教程的链接,可以详细说明“messageHash”函数应该是什么吗? 我建议首先使用 NSObject hash 属性,即在 messageHash 中只有 return self.hash,因为这是现在在 JSQ 库中实现的默认行为。您可能需要注意这里的问题:github.com/jessesquires/JSQMessagesViewController/issues/631,其中列出了您可能希望使用不同实现的原因,但这取决于您的特定设置。 谢谢,这个错误没有重复,但现在出现了一个新错误,它说“无效参数不满足:messageSenderId!= nil”,错误在 Pods/JSQMessagesViewController/JSQMessagesViewController/Controllers/JSQMessagesViewController .m:452 这意味着senderId 没有被设置,无论是在MessageViewController 还是在消息本身,这取决于错误发生的位置。我建议将此作为一个新问题,尽管我怀疑问题再次在于 XBChatModule 尚未更新。 好的@michaerccurtis。谢谢你的帮助。我正在尝试自己解决它,否则我将在明天发布。

以上是关于-[XBMessage messageHash]:发送到实例的无法识别的选择器的主要内容,如果未能解决你的问题,请参考以下文章