xcode 调用类别方法时发送到实例的无法识别的选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6732036/
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-09-14 21:30:42  来源:igfitidea点击:

Unrecognized selector sent to instance when calling category method

iphoneobjective-cxcode

提问by Radu

I have a static library that i made for encryption an XML serialization with i use in my projects. This code worked perfectly until now. but when i included it in my latest project i got an error, i know this error usually appears when the object i call is not properly allocated witch is not the case here the NSLog returns the NSData for my encryption.

我有一个静态库,用于加密我在项目中使用的 XML 序列化。这段代码直到现在都运行良好。但是当我将它包含在我的最新项目中时,我遇到了一个错误,我知道当我调用的对象没有正确分配时,通常会出现这个错误,但在这种情况下,NSLog 返回 NSData 进行加密。

What could be the problem?

可能是什么问题呢?

The error i get is

我得到的错误是

-[NSConcreteData base64EncodingWithLineLength:]: unrecognized selector sent to instance 0x1c9150

*Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData base64EncodingWithLineLength:]: unrecognized selector sent to instance 0x1c9150'

-[NSConcreteData base64EncodingWithLineLength:]: 无法识别的选择器发送到实例 0x1c9150

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSConcreteData base64EncodingWithLineLength:]:无法识别的选择器发送到实例 0x1c9150”

Here is my code:

这是我的代码:

NSData * encryptedMsg =[crypto encrypt:MsgEnc key:[accessdata->Certificate dataUsingEncoding:NSUTF8StringEncoding] padding:&padding];
        NSLog(@"encryptedMsg %@",encryptedMsg);
        NSString * msg = [NSString stringWithFormat:@"%@", [encryptedMsg base64EncodingWithLineLength:0]];

回答by sergio

As far as I know, base64EncodingWithLineLengthis a method that is not defined in NSDatabut in a category called NSData+Base64.h. The reason why you get the error is that you did not add that category to your project, so that the method is called, it is not found.

据我所知,base64EncodingWithLineLength是一种没有定义的方法,NSData而是在一个名为NSData+Base64.h. 报错的原因是你没有把那个category添加到你的项目中,所以调用了这个方法,没有找到。

So you should add "NSData+Base64.*" files to your project. Get them from here.

所以你应该将“NSData+Base64.*”文件添加到你的项目中。从这里拿走它们。

EDIT:

编辑:

Since the OP mention that the category is included in a static library and assuming the static library is correctly linked, then a possible fix for this issue is adding the

由于 OP 提到该类别包含在静态库中并假设静态库已正确链接,因此此问题的可能解决方法是添加

-ObjC

flag to the "Other linker flags" in your Build Settings. This flag will force the loading of all symbols in Objective C categories, preventing them from being optimized out through the linker.

标记到您的构建设置中的“其他链接器标志”。此标志将强制加载目标 C 类别中的所有符号,防止它们通过链接器进行优化。

回答by ZhangChn

I'm afraid base64EncodingWithLineLength:is a Category method attached onto NSData. That means you should compile/link against the code that implements base64EncodingWithLineLengthfor NSDatacategory.

恐怕base64EncodingWithLineLength:是一个 Category 方法附加到NSData. 这意味着你应该编译/链接对代码实现base64EncodingWithLineLengthNSData类别。

回答by Leszek Zarna

It can help if category refers to Core Data entity: set class for the entity in the managed object model.

如果类别是指核心数据实体,则它会有所帮助:为托管对象模型中的实体设置类。