ios - FCNDEF消息查询NDEFStatusWithCompletionHandler :]: unrecognized selector sent to instance

标签 ios objective-c nfc phonegap

我正在尝试获取有关 NFC 标签的一些信息, session 成功启动并在读取标签后,我们跳转到以下函数:

- (void) readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<NSObject<NFCNDEFTag> * > *)tags {
    NSLog(@"NFCNDEFReaderSession didDetectNDEFs");

    for (NSObject<NFCNDEFTag> *tag in tags) {
        [tag queryNDEFStatusWithCompletionHandler:^(NFCNDEFStatus status, NSUInteger capacity, NSError * _Nullable error) {
            if (!error) {
                NSLog(@"success");
            }
        }];
    }
}

我们甚至在循环中进行了迭代,但在我尝试获取有关我的标签的一些信息的那一刻,应用程序崩溃并显示以下错误消息:

019-11-06 16:01:12.955810+0100 MyApp[336:10691] -[NFCNDEFMessage queryNDEFStatusWithCompletionHandler:]: unrecognized selector sent to instance 0x281cfce30
2019-11-06 16:01:12.956200+0100 MyApp[336:10691] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NFCNDEFMessage queryNDEFStatusWithCompletionHandler:]: unrecognized selector sent to instance 0x281cfce30'
*** First throw call stack:
(0x18e45180c 0x18e179fa4 0x18e35536c 0x18e455c88 0x18e457a6c 0x100636cc0 0x100b097fc 0x100b0abd8 0x100b11b48 0x100b12718 0x100b1dadc 0x18e16ef88 0x18e171ad4)
libc++abi.dylib: terminating with uncaught exception of type NSException

注意: NFCNDEFTag 是一个协议(protocol),所以它可能与我声明它的方式有关:NSObject<NFCNDEFTag>

最佳答案

让我们看看 NFCNDEFReaderSessionDelegate 的文档.

你有:

- (void)readerSession:(NFCNDEFReaderSession *)session 
       didDetectNDEFs:(NSArray<NFCNDEFMessage *> *)messages;

- (void)readerSession:(NFCNDEFReaderSession *)session 
        didDetectTags:(NSArray<__kindof id<NFCNDEFTag>> *)tags;

你写道:

- (void)readerSession:(NFCNDEFReaderSession *)session 
        didDetectNDEFs:(NSArray<NSObject<NFCNDEFTag> * > *)tags {

这是两者的混合。

但在 Objective-C 中,选择器将是: readerSession:didDetectNDEFs & readerSession:didDetectTags:为官方的,你的是readerSession:didDetectNDEFs,和第一个一样。所以你引诱了编译器和运行时。

但它不关心参数的类型,所以它不是正确的对象,所以它没有实现方法 queryNDEFStatusWithCompletionHandler:,因此它崩溃并显示该错误消息。

如何解决?

实现真正的委托(delegate)方法,用好参数类型,使用正确的方法,不能随意修改。

关于ios - FCNDEF消息查询NDEFStatusWithCompletionHandler :]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733108/

相关文章:

java - NFC enableForegroundDispatch 崩溃应用程序

iphone - 如何应对所有贬值

ios - 流套接字连接中的 SSL

ios - Kudan:是否可以在检测到标记时调用函数?

iphone - iOS:有条件地加载 View Controller

android - 使用支持 NFC 的 Android 手机读取标签

nfc - NFC 标签是否具有唯一的只读 ID?

ios - 应用程序启动时的 EXC_ARM_BREAKPOINT - 仅当在设备上运行时

objective-c - NSString 通过删除初始零?

objective-c - NSTask 阻塞主线程