Reputation:
I just tried out the following code segment and im having an error :-
#import "JSONKit.h"
#import "Base64.h"
#import <Foundation/Foundation.h>
int main() {
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:(id)@"value" forKey:(id)@"key"];
for(id key in dict) NSLog(@"%@\t%@\n", key, [dict objectForKey:key]);
[Base64 initialize];
NSData *jsonstr = [dict JSONStringWithOptions:JKSerializeOptionNone error:nil];
NSString *val = [Base64 encode: jsonstr];
NSLog(@"%@\n", val);
return 0;
}
The error I got is :-
test.m: In function ‘main’:
test.m:13: warning: incompatible Objective-C types initializing ‘struct NSString *’, expected ‘struct NSData *’
Undefined symbols:
"_OBJC_CLASS_$_Base64", referenced from:
__objc_classrefs__DATA@0 in cc23xlpr.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
In order to remove the warning, I changed a line to :-
NSData *jsonstr = (NSData *)[dict JSONStringWithOptions:JKSerializeOptionNone error:nil];
However, the error remains -- being new to objective-C I find the error quite cryptic, could someone help me understand the source of the problem/a solution to the same?
Upvotes: 0
Views: 272
Reputation: 2087
I guess, that [dict JSONStringWithOptions:JKSerializeOptionNone error:nil]
method return the string and if you want to serialize that string into NSData you should do follow:
[Base64 initialize];
NSString *jsonStr = [dict JSONStringWithOptions:JKSerializeOptionNone error:nil];
NSData *jsonstrData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *val = [Base64 encode: jsonstrData];
NSLog(@"%@\n", val);
Upvotes: 1