开发者

How to Prevent Build Warning: " NSData may not respond to 'dataWithBase64EncodedString:' "

开发者 https://www.devze.com 2022-12-20 08:08 出处:网络
The following code produces this build warning: NSDa开发者_StackOverflow社区ta may not respond to

The following code produces this build warning:

NSDa开发者_StackOverflow社区ta 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?


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!)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消