4

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]];
sergio
  • 68,819
  • 11
  • 102
  • 123
Radu
  • 3,434
  • 4
  • 27
  • 38

3 Answers3

12

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • i already did, i checked that twice Could it be some setting problems for the projects? I recently switched to xCode 4.0.2 – Radu Jul 18 '11 at 11:23
  • 3
    Right-click on NSData+Base64.m, get Information for that file and ensure it is added to the target you are compiling... – sergio Jul 18 '11 at 11:25
  • It's included , i don't know what was wrong but it works now, I made a clean, deleted the app of the iphone, and it worked when i rebuild the app, some freak bug i guess. Tanks for the help. In a normal situation of "Unrecognized selector sent to instance" your solution would be the fix for this sow i will mark it as right answer, cheers – Radu Jul 18 '11 at 11:58
  • Same here; it's included. This is happening in the Soomla iOSStore project. They call it and they have it included in their project, which I have a reference to in mine. – Almo Nov 12 '13 at 19:15
  • 1
    @Almo: if you are using a static lib including the NSData extension, make sure it is compiled with the -ObjC and -load_all linker flags. – sergio Nov 13 '13 at 09:14
  • @sergio hey thanks for the tip! It led me to this: http://stackoverflow.com/questions/2567498/objective-c-categories-in-static-library -all_load is no longer needed. Putting -ObjC into the linker options for the main project (not the referenced static lib) fixed the problem. :D – Almo Nov 13 '13 at 13:23
2

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.

Girish
  • 4,692
  • 4
  • 35
  • 55
ZhangChn
  • 3,154
  • 21
  • 41
0

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

Leszek Zarna
  • 3,253
  • 26
  • 26