1

Can someone tell me why i get an error in this method:

+ (NSData *)dataFromBase64String:(NSString *)aString
{
NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding];
size_t outputLength;
void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength);
NSData *result = [NSData dataWithBytes:outputBuffer length:outputLength];
free(outputBuffer);
return result;
}

Error is at void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength); It sais No matching funcion for call to "NewBase64Decode". On other two projects that i have it works fine, but in my project it shows errors.

Karolis Raišelis
  • 335
  • 1
  • 3
  • 10

1 Answers1

0

If you look the original code for Gallagher's NSData category (the link is at the bottom of this page: http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html), you'll see that function included right in the same .m file, so I don't know why it's not finding that function in your case. Perhaps you cut and pasted code, rather than just including the original NSData+Base64.m and NSData+Base64.h files. You might want to re-retrieve the source code.


As an aside, iOS 7 and Mac OS X v10.9 introduced native base-64 methods, eliminating the need for third party base-64 routines:

– initWithBase64EncodedData:options:
– initWithBase64EncodedString:options:
– base64EncodedDataWithOptions:
– base64EncodedStringWithOptions:

They also exposed the following two previously-private methods for backward compatibility with earlier iOS and Mac OS X versions:

– initWithBase64Encoding: 
– base64Encoding

See the "Creating Data Objects" and "Base-64 Encoding" sections of the NSData reference.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Ok, i managed to fix that, but another question is... How should i use it? I'm new to objective c, and its hard for me to code these kind of things.. Could you tell me any strategic tips, how to start? – Karolis Raišelis Dec 12 '13 at 15:36
  • @KarolisRaišelis I don't follow you. Your question shows you the method that you call to convert a base64 string to a `NSData`. [This answer](http://stackoverflow.com/a/19794564/1271826) shows you how you'd do it with the native base-64 iOS routines. I assume if you're pursuing a base-64 that you must have some web service which is delivering base-64 string (or is expecting base-64 string). I'd suggest you post a new question that outlines the specifics of the web service that you're using that employs base-64. (It's certainly beyond the scope of what we should tackle in comments here...) – Rob Dec 12 '13 at 20:03
  • So i found the problem in my compiler, that was not allowing me to use base64 files code. In compiler options at "Compile sources As" it was set to "objective c++", but now when i change that to something else, i get errors in my older code. What should i do? – Karolis Raišelis Dec 13 '13 at 08:42
  • @KarolisRaišelis You might want to change "compile sources as" to "according to file type". – Rob Dec 13 '13 at 12:38
  • that gives me other errors in my older code, and i dont realy want to fix them. – Karolis Raišelis Dec 13 '13 at 12:56
  • @KarolisRaišelis So the other code doesn't conform to standard file extensions? That sucks. Sometimes we're saddled with old code and are reticent to complete refactor it if we don't have to, so I feel your pain. Well, you can cut the Gordian knot and just remove Gallagher's `NSData+Base64` category altogether and just use [the native base 64 functions](http://stackoverflow.com/questions/392464/how-do-i-do-base64-encoding-on-iphone-sdk/19794564#19794564) that Cocoa provides. – Rob Dec 13 '13 at 14:36
  • But that one is in iOS7. I'm still using xcode 4,6 because of some issues that i don't fell like talking. If i upgrade my xcode to iOS7 is there a way to use those those funcions in my iOS6 app? – Karolis Raišelis Dec 13 '13 at 20:23
  • @KarolisRaišelis Yes, if you upgrade to Xcode 5, you will have access to the iOS 7 SDK which has those new functions (and exposes the previously private functions for earlier iOS versions) and [my example](http://stackoverflow.com/questions/392464/how-do-i-do-base64-encoding-on-iphone-sdk/19794564#19794564) illustrates how to do the runtime checks so you call the rendition appropriate for the device's iOS version, by calling `respondsToSelector` rather than checking iOS versions. – Rob Dec 13 '13 at 20:26
  • @KarolisRaišelis Alternatively, if you want to stay with Xcode 4.6 and use the Gallagher NSData category, you can change the "compile sources" option to "according to file type" and then either (a) change the extension of the files that are Objective-C++ to `.mm`; or (b) in the Xcode file navigator, select the Objective-C++ sources and in the Xcode "File Inspector", manually change the "Type" for those other sources to "Objective C++". It's probably the right way to fix the existing project, anyway. – Rob Dec 13 '13 at 21:00