Re: How to code a NSString literal with UTF8?
Re: How to code a NSString literal with UTF8?
- Subject: Re: How to code a NSString literal with UTF8?
- From: j o a r <email@hidden>
- Date: Wed, 30 Mar 2005 19:20:17 +0200
Thank you Ali!
Now, could we *please* have this added to the official documentation
(Scott + Mmalc, are you listening? Somewhere in NSString, and perhaps
to the ObjC docs?), perhaps to some example code snippet included with
the dev tools and finally to all third party Cocoa resource web sites
on the web (Mr. Alastair, Mr. Stevenson...)?
If this question is not a FAQ item, I don't know what is...
People will no doubt still ask this question, but it would be great to
have an authoritative answer to point to! We can of course always use
this:
<http://www.cocoabuilder.com/archive/message/cocoa/2005/3/30/131791>
:-)
j o a r
On 2005-03-30, at 18.59, Ali Ozer wrote:
One possible misconception people come away in these discussions is
that you need to use NSLocalizedString() if you want NSStrings with
non-ASCII characters.
NSLocalizedString and friends are absolutely great choice if you need
localizable strings, that is, strings which will need to be translated
to different languages. The string is read from a .strings file,
which can be made per-language.
However, if all you want is an NSString with some non-ASCII chars in
it, and it's not meant to be shown to the user (and hence doesn't need
to be localized), then it's perfectly fine to create the string
programmatically. One possibility here is:
NSString *s = [NSString stringWithFormat:@"Long %C dash", 0x2014];
You can also do (since \xe2\x80\x94 is the 3-byte UTF-8 string for
0x2014):
NSString *s = [NSString stringWithUTF8String:"Long \xe2\x80\x94
dash"];
but one thing that is not very safe is to include actual high-bit
characters in your source code:
NSString *s = [NSString stringWithUTF8String:"Long — dash"]; //
Not safe; you're at the mercy of any tools you use
and the following is not allowed:
NSString *s = @"Long — dash"; // Not allowed
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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