0

I'm starting to learn Objective-C, and I've tried to compile this code on Debian Wheezy with GCC (GNUStep):

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        NSString *base64String = @"SGVsbG8gV29ybGQh";

        NSURL *URL = [NSURL URLWithString: [NSString stringWithFormat:@"data:;base64,%@", base64String]];

        NSData *decodedData = [NSData dataWithContentsOfURL:URL];

        NSLog(@"%@", decodedData);

        [pool drain];

        return 0;
}

It compiles fine, but it returns "null" every time when I run the application, I've tried different solutions (converting NSData to NSString) but that didn't spit out decoded string neither.

Is it a problem with my code or the GNUStep environment?

Bartosz Wójcik
  • 1,079
  • 2
  • 13
  • 31
  • Your code tries to load data from the URL "data:;base64,SGVsbG8gV29ybGQh". Why do you expect that this *decodes* the Base64 string? – Martin R Jul 02 '14 at 17:45
  • I just took this code from SO question :P – Bartosz Wójcik Jul 02 '14 at 18:16
  • Can you provide a link to that question? I cannot see any Base64 decoding in that code. - On OS X, NSData has built-in methods for Base-64 decoding, but I have no idea if these are available in GNUstep. There are also many 3rd-party libraries available. – Martin R Jul 02 '14 at 18:25
  • Here you go http://stackoverflow.com/questions/19088231/base64-decoding-in-ios-7 – Bartosz Wójcik Jul 02 '14 at 19:22
  • 1
    Aha! That is interesting, I did known know the method described in Tommy's answer http://stackoverflow.com/a/22432808/1187415. - But that is probably not supported in GNUstep. – Martin R Jul 02 '14 at 19:27

1 Answers1

0

Basically the url which you are passing should specify the valid location for reading the data. So if you just want to convert string into data then follow below:-

  NSData* decodedData = [ base64String dataUsingEncoding:NSUTF8StringEncoding];
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56