2

I am using a UILocalizedIndexedCollation to get the array of local sectionIndexTitles. Now I want to check a string if it starts with some element from these sectionIndexTitles.

Assuming my language is Korean, sectionIndexTitles are this:

"ㄱ,ㄴ,ㄷ,ㄹ,ㅁ,ㅂ,ㅅ,ㅇ,ㅈ,ㅊ,ㅋ,ㅌ,ㅍ,ㅎ,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,#" 

Question

How do I check if the String "나비" starts with "ㄴ"?

The problem is that the first "character" is "나" and "ㄴ" never stands by itself in a character. So it seems like the sectionIndexTitles should actually contain first character-combinations as e.g. .

"ㄴ".characters.first // "ㄴ"
"나비".characters.first // "나"
"나비".hasPrefix("ㄴ") // false
"나비".localizedStandardRange(of: "ㄴ") // nil

Update, following up on your comment:

"나비".range(of: "ㄴ") // nil

Update 2: Question

How can I create alphabetic section headers like "A", "B", "C", ... and put in localized string elements like "나비"?

In e.g. Korean there are various combinations for the first "character", so the sectionIndexTitles from UILocalizedIndexedCollation (e.g. "ㄴ") can't be used to sort elements into the right section

MarkHim
  • 5,686
  • 5
  • 32
  • 64
  • sorry but `"나비".range(of: "ㄴ")` what does it return? (just asking. I never had this kind of problems, but your question is really interesting! it's not quite common..) – ddb Aug 30 '16 at 12:10
  • maybe [this answer](http://stackoverflow.com/a/29224218/3178454) and [this one](http://stackoverflow.com/a/33418270/3178454) may help – ddb Aug 30 '16 at 12:13
  • @ddb thanks, I updated my question after I tried out `range` – MarkHim Aug 30 '16 at 12:16
  • @MuhammadAdnan I am using the sectionIndexTitles for scrolling to an element in a collectionView -> I don't have any more information than "ㄴ" and it's impossible to compare with all combinations that start with "ㄴ" – MarkHim Aug 30 '16 at 12:25

1 Answers1

1

Zooming on characters, I see that "나" and "ㄴ" start with two different visual elements.

The "L" visual element in the first case has a curved horizontal part

enter image description here

while in the second (the letter you're trying to match) doesn't.

enter image description here

So I think that, if you want to hierarchically organize data, strings that starts with "나" should be simply included in the "#" section (as far as I understand studying UILocalizedIndexedCollation at this tutorial).

Basically if "나비".hasPrefix("ㄴ") is false, then it is false... and so it should be handled.

ddb
  • 2,423
  • 7
  • 28
  • 38
  • 1
    These are 2 fundamentally different strings: `\u{b098}\u{be44}` vs. `\u{3134}`. The fact that they both start as an L-like shape is irrelevant. It's like I (uppercase i) and l (lowercase ell) look the same in Arial doesn't mean that they are equal – Code Different Aug 30 '16 at 12:35
  • That means that the `sectionIndexTitles` don't actually return valid first characters and is therefore not useful for e.g section headers. – MarkHim Aug 30 '16 at 13:48
  • @MarkHim, I think yes – ddb Aug 30 '16 at 13:50