views:

618

answers:

3

The following code produces this build warning:

NSData may not respond to 'dataWithBase64EncodedString:'

The code:

NSString * message = @"string string string";

NSData *data= [NSData  dataWithBase64EncodedString:(NSString *)message];

How do I fix this to remove this warning?

+4  A: 

Removing the warning is the least of your worries - NSData doesn't respond to that method and this code will crash if you run it!

See the docs here for the default available methods on NSData.

However, you're probably looking for this page which has an implementation of dataWithBase64EncodedString in a category (see the very last post on the thread!)

deanWombourne
+3  A: 

NSData does not have dataWithBase64EncodedString: method. If you use some custom NSData category with this method you should import header where it is defined.

Edit: So if you're using code from this link then you can just create your own .h and .m files and copy that code into them:

// MBBase64.h 
@interface NSData (MBBase64)
    + (id)dataWithBase64EncodedString:(NSString *)string;     //  Padding '=' characters are optional. Whitespace is ignored.
    - (NSString *)base64Encoding;
@end

//MBBase64.m
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

@implementation NSData (MBBase64)
...
@end

And then import MBBase64.h header wherever you want to use +dataWithBase64EncodedString: method.

Vladimir
but which header i should import?????
header where -dataWithBase64EncodedString: method is defined, how can anyone now the structure of your project/ 3rd headers you have etc?
Vladimir
ok.i understand ur answerbut which header file, i should import????
plz give me 1 example of it .if possible bcoz i didn't understand ur answer 100%
Could you say at least where did you get that dataWithBase64EncodedString method from? How can we guess the name of the header you need?
Vladimir
+2  A: 

Just to clarify the previous answers:

In the NSFoundation API, NSData does not have a dataWithBase64EncodedString: method. If your copying code in which it does, then that code has extended NSData by adding to it a category that contains the method.

You can add arbitrary methods to any class using a category. If someone has used a category in their example code, you cannot use that code unless you also get the header and implementation files that define the category. If the original author did not make those available then you are out of luck.

Base64 encoding isn't one of the API provided encodings for strings so you'll probably have to implement that yourself or find some code by someone who has.

TechZen
ok.i understand ur answer but which header file, i should import???? plz give me 1 example of it .if possible bcoz i didn't understand ur answer 100% – user217572 0 secs ago
The header file you need is the custom header created by the programmer who created the category. Categories can extend the methods of any class, including those of the Apple supplied API. Anyone in the entire world could have written that category. It's probably out there somewhere on the internet but unless you know who wrote and where they made it available, you have no hope at all of obtaining the header. Your best bet would be to go back to where you found the example of the method in the first place. Otherwise, you will have to write your own method.
TechZen