Radu
Radu

Reputation: 3494

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]];

Upvotes: 4

Views: 8216

Answers (3)

Leszek Zarna
Leszek Zarna

Reputation: 3317

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

Upvotes: 0

sergio
sergio

Reputation: 69027

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.

Upvotes: 12

ZhangChn
ZhangChn

Reputation: 3184

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.

Upvotes: 2

Related Questions