Re: (newbie question) Putting special characters in NSStrings
Re: (newbie question) Putting special characters in NSStrings
- Subject: Re: (newbie question) Putting special characters in NSStrings
- From: Clark Cox <email@hidden>
- Date: Mon, 21 Mar 2005 10:45:21 -0500
On Mon, 21 Mar 2005 10:29:02 -0500, Sean McBride
<email@hidden> wrote:
> On 2005-03-21 10:21, Clark Cox said:
>
> >> So is it invalid Obj-C to use non-ASCII strings in source, or is it
> >> disallowed just because gcc is not C-compliant, or is Obj-C not C99-
> >> based, or what? I do wish there was an ISO Obj-C standard. :)
> >
> >Obj-C is based on whatever C your compiler supports, and currently,
> >GCC does not support the Universal Character Name feature of C99.
>
> So is it correct to say that using @"±§£€" is (linguistically) valid Obj-
> C but just not supported by gcc?
No, as in Apple's Objective-C documentation, the @"" construct can
only take ASCII characters. But, if you know that the source file is
in UTF-8, then [NSString stringWithUTF8String: "±§£€"] will get you
what you expect (that is, an NSString containing the four characters
"±§£€").
What I meant by the "Universal Character Name" feature is that GCC
does not yet support using the \uXXXX or \UXXXXXXXX syntax in source
code.
So, if you want to get non-ASCII characters into an NSString, you have
several choices:
1) For very short strings:
[NSString stringWithFormat: @"%C%C%C%C", 0xB1, 0xA7, 0xA3, 0x20AC];
2) Explicitly use UTF-8 octets:
[NSString stringWithUTF8String: "\xC2\xB1\xC2\xA7\xC2\xA3\xE2\x82\xAC"];
3) Read from some .strings file
NSLocalizedString(@"SomeKey",@"")
4) If you are absolutely sure that the source code is UTF-8:
[NSString stringWithUTF8String: "±§£€"];
Of these, 1,2 and 3 will work regardless of the source code encoding,
while 4 requires that you know it explicitly.
--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden