Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized selector sent to instance when calling category method

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.

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'

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]];
like image 839
Radu Avatar asked Dec 10 '22 07:12

Radu


2 Answers

As far as I know, base64EncodingWithLineLength is a method that is not defined in NSData but 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.

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

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

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

like image 72
sergio Avatar answered Dec 30 '22 10:12

sergio


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

like image 22
ZhangChn Avatar answered Dec 30 '22 11:12

ZhangChn