JSON.framework add in xcode - iphone

I have copy JSON.framework in my
Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk/System/Library/Frameworks
both folder and importing
#import <JSON/SBJsonParser.h>
but when I am using it giving me linking error error
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
Ld build/Debug-iphonesimulator/HelloThere.app/HelloThere normal i386
cd /Users/samargupta/Desktop/hellothere
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk -L/Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator -F/Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator -filelist /Users/samargupta/Desktop/hellothere/build/HelloThere.build/Debug-iphonesimulator/HelloThere.build/Objects-normal/i386/HelloThere.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework Foundation -framework UIKit -framework CoreGraphics -framework CoreLocation -framework MapKit -framework JSON -o /Users/samargupta/Desktop/hellothere/build/Debug-iphonesimulator/HelloThere.app/HelloThere
ld: framework not found JSON
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
plz tell me where I am wrong??

I wrote a JSON-parser by myself. You can use it if you want to by copying the code into your class source.
You use it in this way:
NSObject *obj = [self jsonToObj:yourJsonString];
It returns a NSDictionary, NSString or NSArray depending on the input. The difference between my parser and others is, that it also accepts numerical keys, which are not allowed in the JSON-standard. I can't guarantee that there are no bugs in it or that it works with all JSON-strings. Please give me some feedback, if it works for you!
-(NSObject*)jsonToObj:(NSString *)json {
int nextOffset;
return [self jsonToObj:json offset:0 nextOffset:&nextOffset];
}
static int indexOfChar(NSString *str, unichar c, int offset) {
int length = [str length];
for (int i = offset; i < length; i++) {
if ([str characterAtIndex:i] == c) {
return i;
}
}
return -1;
}
static int indexOfNonWhitespace(NSString *str, int offset) {
int len = [str length];
NSCharacterSet *cSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for (int i = offset; i < len; i++) {
if (![cSet characterIsMember:[str characterAtIndex:i]]) {
return i;
}
}
return -1;
}
static int indexOfNonNumber(NSString *str, int offset) {
int len = [str length];
for (int i = offset; i < len; i++) {
unichar c = [str characterAtIndex:i];
if (c != '-' && c != '.' && (c < '0' || c > '9')) return i;
}
return -1;
}
static int parseHex(NSString *hexStr) {
unsigned int result = 0;
int len = [hexStr length];
for (int i = 0; i < len; i++) {
result <<= 4;
unsigned int v = 0;
unichar c = [hexStr characterAtIndex:i];
if (c >= '0' && c <= '9') {
v = c-'0';
} else if (c >= 'a' && c <= 'f') {
v = c-'a'+10;
} else if (c >= 'A' && c <= 'F') {
v = c-'A'+10;
}
result += v;
}
return result;
}
-(NSObject*)jsonToObj:(NSString *)json offset:(int)offset nextOffset:(int*)nextOffset {
offset = indexOfNonWhitespace(json, offset);
static NSString *jsonExceptionName = #"InvalidJSONException";
if (offset == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"no non-whitespace 1" userInfo:nil];
}
unichar startChar = [json characterAtIndex:offset];
if (startChar == '{') {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
int curPos = offset+1;
while (YES) {
curPos = indexOfNonWhitespace(json, curPos);
if (curPos == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"no non-whitespace 2" userInfo:nil];
}
unichar curChar = [json characterAtIndex:curPos];
if (curChar == ',') {
curPos++;
continue;
} else if (curChar == '}') {
*nextOffset = curPos+1;
return result;
}
unichar quotChar = curChar;
if ((quotChar != '\'' && quotChar != '"') || [json characterAtIndex:curPos-1]=='\\') {
quotChar = 0;
}
NSString *key = nil;
int semiIndex = 0;
if (quotChar != 0) {
int quotStart = curPos+1;
int quotEnd = quotStart;
while (YES) {
quotEnd = indexOfChar(json, quotChar, quotEnd);
if (quotEnd == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"quotation-end not found 1" userInfo:nil];
}
if ([json characterAtIndex:quotEnd-1] != '\\') break;
else quotEnd++;
}
key = [json substringWithRange:NSMakeRange(quotStart, quotEnd-quotStart)];
semiIndex = indexOfChar(json, ':', quotEnd);
if (semiIndex == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"semicolon not found 1" userInfo:nil];
}
} else {
semiIndex = indexOfChar(json, ':', curPos);
if (semiIndex == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"semicolon not found 2" userInfo:nil];
}
key = [[json substringWithRange:NSMakeRange(curPos, semiIndex-curPos)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
[result setObject:[self jsonToObj:json offset:semiIndex+1 nextOffset:&curPos] forKey:key];
}
} else if (startChar == '[') {
NSMutableArray *result = [NSMutableArray array];
int curPos = offset+1;
while (YES) {
curPos = indexOfNonWhitespace(json, curPos);
if (curPos == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"no non-whitespace 3" userInfo:nil];
}
unichar curChar = [json characterAtIndex:curPos];
if (curChar == ',') {
curPos++;
continue;
} else if (curChar == ']') {
*nextOffset = curPos+1;
return result;
}
[result addObject:[self jsonToObj:json offset:curPos nextOffset:&curPos]];
}
} else {
unichar quotChar = startChar;
if ((quotChar != '\'' && quotChar != '"') || [json characterAtIndex:offset-1]=='\\') {
quotChar = 0;
}
if (quotChar != 0) {
int quotStart = offset+1;
int quotEnd = quotStart;
while (YES) {
quotEnd = indexOfChar(json, quotChar, quotEnd);
if (quotEnd == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"quotation-end not found 2" userInfo:nil];
}
if ([json characterAtIndex:quotEnd-1] != '\\') break;
else quotEnd++;
}
*nextOffset = quotEnd+1;
NSString *str = [json substringWithRange:NSMakeRange(quotStart, quotEnd-quotStart)];
str = [str stringByReplacingOccurrencesOfString:#"\\\"" withString:#"\""];
NSMutableString *result = [NSMutableString stringWithCapacity:[str length]];
int len = [str length];
for (int i = 0; i < len; i++) {
unichar c = [str characterAtIndex:i];
if (c != '\\') {
[result appendString:[NSString stringWithCharacters:&c length:1]];
} else if (i+1 < len) {
unichar c2 = [str characterAtIndex:i+1];
if (c2 == '\\') {
[result appendString:[NSString stringWithCharacters:&c2 length:1]];
i++;
} else if (c2 == 'u') {
unichar cUni = parseHex([str substringWithRange:NSMakeRange(i+2, 4)]);
[result appendString:[NSString stringWithCharacters:&cUni length:1]];
i += 5;
}
}
}
return result;
} else {
int numberEnd = indexOfNonNumber(json, offset);
if (numberEnd == -1) {
#throw [NSException exceptionWithName:jsonExceptionName reason:#"number-end not found" userInfo:nil];
}
//BOOL isFloat = indexOfChar(json, '.', offset)!=-1;
NSString *numberStr = [json substringWithRange:NSMakeRange(offset, numberEnd-offset)];
*nextOffset = numberEnd;
return numberStr;
}
}
}

Just create a folder in your XCode project name it say "JSON". All the .h and .m files provided by the JSON framework must be in that and well include them in your app, build, run etc etc. Thats how.

The framework folder 'JSON.framework' must be added under each SDK framework folder that you wish to use (iPhone Simulator, iPhoneOS 4.3), or your build will fail with the error that you reported. Alternatively you can just have the folder linked out of your project folder as Bourne mentioned. Either way it Xcode needs to know where the framework is for each type of build your compiling.

Related

saving images in string format (to use in xml)..not working

i am converting image from picker into nsdata(jpeg representation) and then converting it into nsstring using the following code
NSData *data=UIImageJPEGRepresentation(image,1.0);
NSString *imageString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
[[NSUserDefaults standardUserDefaults] setObject:imageString forKey:#"image_name"];
and at the other end where i need to display the image the uiimage is formed as follows.
NSString *imageString=[[NSString alloc] init];
imageString=[[NSUserDefaults standardUserDefaults] objectForKey:#"image_name"];
UIImage *image=[UIImage imageWithData:[imageString dataUsingEncoding:NSUTF8StringEncoding]];
the image variable used in the top code is not nil ,but the image formed from data is getting nil...when i nslogged userdefaults some string is present for the key mentioned above .can anyone explain why is this so..what is the right way to do this
If it goes through a web server or the like, you could encapsulate it with base64 enc/decoding or some other plain encoder.
It removes "bad" char, ie that screw up the string during transformation, and change them to generic alphabetical chars and then back again.
if this is the reason to your issues, here is a short one i use (which I most probably stole and adapted, but do not remember from whom. Sorry! :-) )
base64helper.h
#import <Foundation/Foundation.h>
#interface NSData (MBBase64)
base64helper.m
#import "base64helper.h"
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#implementation NSData (MBBase64)
+ (id)dataWithBase64EncodedString:(NSString *)string;
{
if (string == nil)
[NSException raise:NSInvalidArgumentException format:nil];
if ([string length] == 0)
return [NSData data];
static char *decodingTable = NULL;
if (decodingTable == NULL)
{
decodingTable = malloc(256);
if (decodingTable == NULL)
return nil;
memset(decodingTable, CHAR_MAX, 256);
NSUInteger i;
for (i = 0; i < 64; i++)
decodingTable[(short)encodingTable[i]] = i;
}
const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
if (characters == NULL) // Not an ASCII string!
return nil;
char *bytes = malloc((([string length] + 3) / 4) * 3);
if (bytes == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (YES)
{
char buffer[4];
short bufferLength;
for (bufferLength = 0; bufferLength < 4; i++)
{
if (characters[i] == '\0')
break;
if (isspace(characters[i]) || characters[i] == '=')
continue;
buffer[bufferLength] = decodingTable[(short)characters[i]];
if (buffer[bufferLength++] == CHAR_MAX) // Illegal character!
{
free(bytes);
return nil;
}
}
if (bufferLength == 0)
break;
if (bufferLength == 1) // At least two characters are needed to produce one byte!
{
free(bytes);
return nil;
}
// Decode the characters in the buffer to bytes.
bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
if (bufferLength > 2)
bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
if (bufferLength > 3)
bytes[length++] = (buffer[2] << 6) | buffer[3];
}
realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
- (NSString *)base64Encoding;
{
if ([self length] == 0)
return #"";
char *characters = malloc((([self length] + 2) / 3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [self length])
{
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < [self length])
buffer[bufferLength++] = ((char *)[self bytes])[i++];
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}
#end

Hyphenation library doesn't work with iOS 5

I just tried the hyphenate library of Tupil.
It was mentioned here http://blog.tupil.com/adding-hyphenation-to-nsstring/.
But while it is working perfectly under iOS 4.3, I did not get it to work with iOS 5.
Are there any other frameworks I could use? I heard of CoreText, but I don't know where to start.
Thanks in advance
Martin
I realize it's been a few years, but I just found that there's a Core Foundation function that suggests hyphenation points: CFStringGetHyphenationLocationBeforeIndex. It only works for a few languages, but it looks like it might be really helpful for the narrow label problem.
Update:
Here is some example code. It's a CLI program that shows where to hyphenate a word:
#include <Cocoa/Cocoa.h>
int main(int ac, char *av[])
{
#autoreleasepool {
if(ac < 2) {
fprintf(stderr, "usage: hyph word\n");
exit(1);
}
NSString *word = [NSString stringWithUTF8String: av[1]];
unsigned char hyspots[word.length];
memset(hyspots, 0, word.length);
CFRange range = CFRangeMake(0, word.length);
CFLocaleRef locale = CFLocaleCreate(NULL, CFSTR("en_US"));
for(int i = 0; i < word.length; i++) {
int x = CFStringGetHyphenationLocationBeforeIndex(
(CFStringRef) word, i, range,
0, locale, NULL);
if(x >= 0 && x < word.length)
hyspots[x] = 1;
}
for(int i = 0; i < word.length; i++) {
if(hyspots[i]) putchar('-');
printf("%s", [[word substringWithRange: NSMakeRange(i, 1)] UTF8String]);
}
putchar('\n');
}
exit(0);
}
Here's how it looks when you build and run it:
$ cc -o hyph hyph.m -framework Cocoa
$ hyph accessibility
ac-ces-si-bil-i-ty
$ hyph hypothesis
hy-poth-e-sis
These hyphenations agree exactly with the OS X dictionary. I am using this for a narrow label problem in iOS, and it's working well for me.
I wrote a category based Jeffrey's answer for adding "soft hyphenation" to any string. These are "-" which is not visible when rendered, but instead merely queues for CoreText or UITextKit to know how to break up words.
NSString *string = #"accessibility tests and frameworks checking";
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
NSString *hyphenatedString = [string softHyphenatedStringWithLocale:locale error:nil];
NSLog(#"%#", hyphenatedString);
Outputs ac-ces-si-bil-i-ty tests and frame-works check-ing
NSString+SoftHyphenation.h
typedef enum {
NSStringSoftHyphenationErrorNotAvailableForLocale
} NSStringSoftHyphenationError;
extern NSString * const NSStringSoftHyphenationErrorDomain;
extern NSString * const NSStringSoftHyphenationToken;
#interface NSString (SoftHyphenation)
- (BOOL)canSoftHyphenateStringWithLocale:(NSLocale *)locale;
- (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error;
#end
NSString+SoftHyphenation.m
#import "NSString+SoftHyphenation.h"
NSString * const NSStringSoftHyphenationErrorDomain = #"NSStringSoftHyphenationErrorDomain";
NSString * const NSStringSoftHyphenationToken = #"­"; // NOTE: UTF-8 soft hyphen!
#implementation NSString (SoftHyphenation)
- (BOOL)canSoftHyphenateStringWithLocale:(NSLocale *)locale
{
CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale);
return CFStringIsHyphenationAvailableForLocale(localeRef);
}
- (NSString *)softHyphenatedStringWithLocale:(NSLocale *)locale error:(out NSError **)error
{
if(![self canSoftHyphenateStringWithLocale:locale])
{
if(error != NULL)
{
*error = [self hyphen_createOnlyError];
}
return [self copy];
}
else
{
NSMutableString *string = [self mutableCopy];
unsigned char hyphenationLocations[string.length];
memset(hyphenationLocations, 0, string.length);
CFRange range = CFRangeMake(0, string.length);
CFLocaleRef localeRef = (__bridge CFLocaleRef)(locale);
for(int i = 0; i < string.length; i++)
{
CFIndex location = CFStringGetHyphenationLocationBeforeIndex((CFStringRef)string, i, range, 0, localeRef, NULL);
if(location >= 0 && location < string.length)
{
hyphenationLocations[location] = 1;
}
}
for(int i = string.length - 1; i > 0; i--)
{
if(hyphenationLocations[i])
{
[string insertString:NSStringSoftHyphenationToken atIndex:i];
}
}
if(error != NULL) { *error = nil; }
// Left here in case you want to test with visible results
// return [string stringByReplacingOccurrencesOfString:NSStringSoftHyphenationToken withString:#"-"];
return string;
}
}
- (NSError *)hyphen_createOnlyError
{
NSDictionary *userInfo = #{
NSLocalizedDescriptionKey: #"Hyphenation is not available for given locale",
NSLocalizedFailureReasonErrorKey: #"Hyphenation is not available for given locale",
NSLocalizedRecoverySuggestionErrorKey: #"You could try using a different locale even though it might not be 100% correct"
};
return [NSError errorWithDomain:NSStringSoftHyphenationErrorDomain code:NSStringSoftHyphenationErrorNotAvailableForLocale userInfo:userInfo];
}
#end
:)

iPhone NSString comparison having Extended ASCII or Latin 1 Unicode character set

I am having problem with NSString compare. The localizedCaseInsensitiveCompare: or any other compare: method in NSString is returning wrong result to me with extended ascii character set. for example below code, after comparison, noString should be after the enString1.
any idea, how can I solve the problem?
NSString * noString = #"ÅåÆæØøab"; // ASCII values, 197, 229, 198, 230, 216, 248, 97, 98
NSString * enString1 = #"fbcd";
NSString * enString2 = #"bbcd";
NSString * enString3 = #"zbcd";
NSLog(#"%i", [enString1 localizedCaseInsensitiveCompare:enString2]); // 1
NSLog(#"%i", [enString1 localizedStandardCompare:noString]); // 1, should be -1
NSLog(#"%i", [enString1 localizedCaseInsensitiveCompare:noString]); // 1, should be -1
"aa" compareTo "aå" is returning Ascending. Thats right. But,
"aa" compareTo "aæ" is returning Descending, why? same for "aø" too.
The following way can be solution, but it works for comparison with å but not for ø and æ.
NSLog(#"%i", [enString1 compare:noString options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [enString1 length])
locale:[[NSLocale alloc] initWithLocaleIdentifier:#"no"] ]);
Find here More on string operations
If anybody stubmle upon this question, this is how I made a solution.
It seems there is still some problem with non-english characters in NSString comparison. Specially for Å, Ø and Æ etc. The sorting order seems AÅ...ÆØ even with localized compare. THough ASCII code is different. So I wrote my own comparison method,
- (NSComparisonResult) caseInsensitiveStringCompare:(NSString *)string1
anotherString:(NSString *)string2
{
NSString * ciString1 = [string1 lowercaseString];
NSString * ciString2 = [string2 lowercaseString];
return [self stringCompare:ciString1 anotherString:ciString2];
}
- (NSComparisonResult) stringCompare:(NSString *)string1
anotherString:(NSString *)string2
{
int len1 = [string1 length];
int len2 = [string2 length];
if (len1 < len2) {
int i = 0;
while ( i < len1 && [string1 characterAtIndex:i] == [string2 characterAtIndex:i] )
{
i++;
}
if (i == len1) {
return NSOrderedAscending;
} else if ([string1 characterAtIndex:i] < [string2 characterAtIndex:i]) {
return NSOrderedAscending;
} else {
return NSOrderedDescending;
}
} else { // str1 >= str2 len
int i = 0;
while ( i<len2 && [string1 characterAtIndex:i] == [string2 characterAtIndex:i])
{
i++;
}
if (i == len2 && i == len1) {
return NSOrderedSame;
} else if (i == len2){
return NSOrderedDescending;
} else if ([string1 characterAtIndex:i] < [string2 characterAtIndex:i]) {
return NSOrderedAscending;
} else {
return NSOrderedDescending;
}
}
return NSOrderedSame;
}

Problem UIImage initWithData

I have a NSData with a image decode of Base 64.
I want to convert this data in UIImage but ....
NSData * data = [[NSData alloc] initWithContentsOfFile:#"/Users/.../Library/Caches/images/david.txt"];
UIImage * i = [[UIImage alloc] initWithData:data];
NSLog(#"%#",i);
NSData *datai = UIImagePNGRepresentation(i);
UIImage * i2 = [[UIImage alloc] initWithData:datai];
imagen = [[UIImageView alloc] initWithImage:i2];
[imagen setFrame:CGRectMake(0, 0, 320, 480)];
imagen --> variable
I know that the content in data is correct but in my console :
[Session started at 2011-08-24 20:44:25 +0200.]
2011-08-24 20:44:30.579 P[50194:207] (null)
I have decoded with this Objective C code:
- (NSData *)base64DataFromString: (NSString *)string
{
unsigned long ixtext, lentext;
unsigned char ch, inbuf[3], outbuf[4];
short i, ixinbuf;
Boolean flignore, flendtext = false;
const unsigned char *tempcstring;
NSMutableData *theData;
if (string == nil)
{
return [NSData data];
}
ixtext = 0;
tempcstring = (const unsigned char *)[string UTF8String];
lentext = [string length];
theData = [NSMutableData dataWithCapacity: lentext];
ixinbuf = 0;
while (true)
{
if (ixtext >= lentext)
{
break;
}
ch = tempcstring [ixtext++];
flignore = false;
if ((ch >= 'A') && (ch <= 'Z'))
{
ch = ch - 'A';
}
else if ((ch >= 'a') && (ch <= 'z'))
{
ch = ch - 'a' + 26;
}
else if ((ch >= '0') && (ch <= '9'))
{
ch = ch - '0' + 52;
}
else if (ch == '+')
{
ch = 62;
}
else if (ch == '=')
{
flendtext = true;
}
else if (ch == '/')
{
ch = 63;
}
else
{
flignore = true;
}
if (!flignore)
{
short ctcharsinbuf = 3;
Boolean flbreak = false;
if (flendtext)
{
if (ixinbuf == 0)
{
break;
}
if ((ixinbuf == 1) || (ixinbuf == 2))
{
ctcharsinbuf = 1;
}
else
{
ctcharsinbuf = 2;
}
ixinbuf = 3;
flbreak = true;
}
inbuf [ixinbuf++] = ch;
if (ixinbuf == 4)
{
ixinbuf = 0;
outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);
for (i = 0; i < ctcharsinbuf; i++)
{
[theData appendBytes: &outbuf[i] length: 1];
}
}
if (flbreak)
{
break;
}
}
}
return theData;
}
and i encoded with this Java code:
File f = new File(url);
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f));
int bytes = (int) f.length();
byte[] buffer = new byte[bytes];
int readBytes = bi.read(buffer);
bi.close();
/*BASE 64 ENCODE*/
byte[] encodedString = Base64.encode(buffer);
String image = new String(encodedString, "UTF8");
NSData doesn't magically decode base64, you'll have to do that yourself. There's an example how to do this in this blog post.

converting strings containing \u#### to characters on iOS iPhone

I receive a JSON response in a NSData Variable, and then convert the NSDATA into NSString:
NSString *aStr = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
The problem is this string now contains character sequences of teh format \u####.
(for example, Joe\u2019s).
I want to convert all such occurrences with their single character representation. (for example Joe\u2019s should become Joe's.
Any ideas on how to do this?
Also, could someone point me to a resource where i can learn the right terminology for this?
Thanks a lot!
I looked for a answer to this question for far too long today... finally just bit the bullet and wrote my own category on NSString. Feel free to correct mistakes, I'm not a unicode wizard. This definitely doesn't handle unicode past \uffff but I don't need that in my situation.
#interface NSString (UnicodeAdditions)
-(NSString *) unescapeUnicode;
#end
#implementation NSString (UnicodeAdditions)
-(NSString *) unescapeUnicode {
NSString *input = self;
int x = 0;
NSMutableString *mStr = [NSMutableString string];
do {
unichar c = [input characterAtIndex:x];
if( c == '\\' ) {
unichar c_next = [input characterAtIndex:x+1];
if( c_next == 'u' ) {
unichar accum = 0x0;
int z;
for( z=0; z<4; z++ ) {
unichar thisChar = [input characterAtIndex:x+(2+z)];
int val = 0;
if( thisChar >= 0x30 && thisChar <= 0x39 ) { // 0-9
val = thisChar - 0x30;
}
else if( thisChar >= 0x41 && thisChar <= 0x46 ) { // A-F
val = thisChar - 0x41 + 10;
}
else if( thisChar >= 0x61 && thisChar <= 0x66 ) { // a-f
val = thisChar - 0x61 + 10;
}
if( z ) {
accum <<= 4;
}
accum |= val;
}
NSString *unicode = [NSString stringWithCharacters:&accum length:1];
[mStr appendString:unicode];
x+=6;
}
else {
[mStr appendFormat:#"%c", c];
x++;
}
}
else {
[mStr appendFormat:#"%c", c];
x++;
}
} while( x < [input length] );
return( mStr );
}
#end