Re: Init an NSString
Re: Init an NSString
- Subject: Re: Init an NSString
- From: Chris Ridd <email@hidden>
- Date: Fri, 20 Dec 2002 07:11:58 +0000
On 20/12/02 1:45 am, email@hidden <email@hidden> wrote:
>
Hello!
>
>
I'm still in the steep part of Obj-C learning curve,
>
but it starts to yield results.
>
>
I have a small problem right now.
>
I want to test whether a string S starts with
>
a given prefix.
>
>
If I do:
>
NSString * Prefix = @"####";
>
BOOL result = [S hasPrefix:Prefix];
>
>
I get the right result.
>
>
The problem comes when I want to use a
>
non-ASCII prefix. I have tried the same with
>
a sequence of japanese question marks :????.
>
(I wrote them in ASCII here so that you can read
>
the mail, but these are Unicode values).
>
>
When I use the Japanese input interface in the
>
source code, the first time, I was asked something
>
like:
>
"You are about to enter non-ascii codes in your
>
source. What do you want to do?"
>
And there were 2 options.
>
- Promote to UTF-8
>
- Keep as is (as far as I remember).
>
>
I choosed to promote my code to UTF-8. That's
>
what I always do in BeOS which works in UTF-8
>
by default and I can initialize strings with Japanese
>
characters like:
>
char * string = "Some Japanese text";
>
>
Now, when I am trying to run this code, it
>
simply does not work.
>
In the debugger, I can see the string I am testing,
>
(the one I read from a file) which is displayed
>
correctly in the variables area.
>
But the prefix displays garbage characters.
>
The UTF-8 ???? are replaced with 4 times 3 chars.
>
Something like a&#a&#a&#a&#.
>
>
Result: I cannot detect what I want.
>
>
Well, there is a workaround; read the prefix
>
from a file. But how could I initialize the
>
prefix statically?
I got bitten by this recently, and there was a short thread on this list
(check the archives at cocoa.mamasam.com) about it.
Basically you can only construct NSStrings at compile-time using the @"..."
notation that contain 7-bit characters. Probably US-ASCII.
You've got two options - the first is to use something like localized
strings - the syntax of those files has an escape sequence allowing Unicode
to be entered even in ASCII files, though you can also have the whole file
written in Unicode and encoded in UTF-8.
The second is to build your static strings as arrays of unichar, and build
NSStrings from those at run-time (NSString -initWithCharacters:length:).
That suited my app better than using localizable.strings.
Actually there's a 3rd option - work out the UTF-8 encoding of your string
and embed those bytes in your NSString using \x (I can't remember if that's
the escape sequence or not, but I mean the one that lets you embed arbitrary
bytes in the string) but I think that makes your code pretty unreadable and
your strings become hard to change.
Cheers,
Chris
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.