views:

366

answers:

2

hi, when i try to convert form utf-8 string to NSString like so:

NSString *s = [NSString stringWithUTF8String:"\U0627\U0644\U0641\U0631\U0646"];
NSLog(@"%@", s);

i get the compile error:

incomplete universal character name

note that it sometime just works fine:

NSString *UAE = [NSString stringWithUTF8String:"\U0627\U0644\U0641\U0631\U0646"];
    NSLog(@"%@", UAE);

and the output:

الامارات

so why is that happening? please help.

A: 

incomplete universal character name means that you are missing part of one of the utf8 character that you are trying to write.

UTF8 spans from 1 byte symbols to 4 byte symbols, probably one of yours is longer that you wrote and you are missing 1 byte or such..

Jack
thanks alot Jack,however, im trying to parse the UTF-8 string from a webservice that i have no control over. Is space in the utf-8 string has any thing to do with issue. i mean how can I represent a space char in my case here?thanks again
Nasser
If the string is coming from a webservice, how are you getting a compile-time error?
David Gelhar
actually i manually copied the utf-8 string from the dict returned and test it using the method -stringWithUTF8String, this is when i get compile-time error
Nasser
+1  A: 

\U and \u are not the same thing. The \U escape expects 8 (hex) digits instead of 4.

This should work:

NSString *s = [NSString stringWithUTF8String:"\u0627\u0644\u0641\u0631\u0646"];
David Gelhar
man man man, thank you very much :)
Nasser