7

I'd like to do something like this:

const UniChar KA = 'か';

But XCode spits back "Multi-character constant".

I try to avoid using +characterAtIndex of NSString... I need this to iterate over the kana, like you can iterate over the alphabet (char myCharacter = 'A';)

I was looking at Objective c doesn't like my unichars? but it doesn't really solve it in a nice way for me.

Anyway, I'm trying to put the "tenten" and/or "maru" on top of か, た, etc like か→が, た→だ. Might be a ready made solution for that, in case anyone knows, that'll solve my problem as well.

Community
  • 1
  • 1
Jonny
  • 15,955
  • 18
  • 111
  • 232
  • 1
    "but it doesn't really solve it in a nice way for me." Why not? If you're going to be referring to the か character by the name KA anyway, then why does it matter whether the code setting up the constant uses the か symbol or a hex code? Also, are you trying to replace KA with GA for some reason (some cheap cipher?), or does the input actually have a separate dakuten or handakuten (formal names for tenten and maru, respectively) combining character? In that case what you're really looking at is this thing called "Unicode normalization" - try Google. – Karl Knechtel Jul 25 '11 at 07:18

1 Answers1

6

Source code is usually encoded as UTF-8, which means you can't use 16-bit character literals there. You need to use the escape sequence:

const UniChar KA = '\u30AB';

or specify the value numerically:

const unichar KA = 0x30AB;

(Note: I really have no idea if that's the correct code for the example character you gave.)

I think your only other option is to create a .strings file, which can and should be UTF-16 encoded, and then get the characters into your program using NSLocalizedString.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • It could be "solved" by that, but just as you note - it's just not legible in code, you just don't know if the hex string you type in really is the intended character (without really looking up unicode but it's too error prone). I'm leaning towards the unicode normalization thing Karl suggested, but I might just use something like a NSCharacterSet as a work around. – Jonny Jul 25 '11 at 07:35
  • You can't put 16-bit characters into your source code. Simple as that. I added another possibility -- using localization. – jscs Jul 25 '11 at 07:42
  • Didn't really get what you meant by a strings file here - my current project isn't (and will not be) internationalized. I ended up using two NSCharacterSet:s, then testing a hiragana against either of them. Adding the tenten then by "character++" and the circle with "character += 2". :-P Then again it's funny that it's perfectly OK to write @"これいいね" but 'か' is a no-go. I guess @'か' would be the logical answer, but don't think that would work... – Jonny Jul 26 '11 at 16:08
  • Anyway this is the right answer because what I asked isn't possible. – Jonny Jan 12 '12 at 13:46