在韩语字素簇中搜索或比较
在我当前的 UISearchBarController
实现中,我在 filterContentForSearchText:scope:
委托方法中使用 [NSString Compare:]
来返回基于相关对象的当您开始输入时,将其 name 属性添加到结果 UITableView
上。
到目前为止,这在英语和韩语中效果很好,但我希望能够在 NSString
定义的字符簇中进行搜索。这仅适用于少数语言,韩语就是其中之一。
在英语中,compare:
在您输入每个字母后都会返回新结果,但在韩语中,一旦您完成了可识别的字素簇,就会生成结果。我希望能够通过构成音节的各个元素来搜索我的韩语对象名称属性。
谁能阐明如何解决这个问题?我确信这与手动搜索 UTF16 字符或利用较低级别的类有关。
干杯!
下面是一个不起作用的具体示例:
`NSString *string1 = @"이";
`NSString *string2 = @"ㅣ";
NSRange resultRange = [[string1 decomposedStringWithCanonicalMapping] rangeOfString: [string2 decomposedStringWithCanonicalMapping] options:(NSLiteralSearch)];
结果始终是 NSNotFound,无论是否有 decomposedStringWithCanonicalMapping
。
有什么想法吗?
In my current implementation of a UISearchBarController
I'm using [NSString compare:]
inside the filterContentForSearchText:scope:
delegate method to return relevant objects based on their name property to the results UITableView
as you start typing.
So far this works great in English and Korean, but what I'd like to be able to do is search within NSString
's defined character clusters. This is only applicable for a handfull of languages, of which Korean is one.
In English, compare:
returns new results after every letter you enter, but in Korean the results are generated once you complete a recognized grapheme cluster. I would like to be able to search through my Korean objects name property via the individual elements that make up a syllable.
Can anyone shed any light on how to approach this? I'm sure it has something to do with searching through UTF16 characters manually, or by utilising a lower level class.
Cheers!
Here is a specific example that's just not working:
`NSString *string1 = @"이";
`NSString *string2 = @"ㅣ";
NSRange resultRange = [[string1 decomposedStringWithCanonicalMapping] rangeOfString: [string2 decomposedStringWithCanonicalMapping] options:(NSLiteralSearch)];
The result is always NSNotFound, with or without decomposedStringWithCanonicalMapping
.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是专家,但我认为您不太可能找到适合您想要的解决方案。韩语字符的 Unicode 值与其组成的字素之间似乎没有任何关系。
例如,“æ”是 \uc774,“ㅣ”是 \u3163。从 NSString 的角度来看,它们只是两个不同的字符,彼此之间没有特定的关系。
我怀疑您必须找到或创建字符及其字素之间的显式映射,然后编写自己的搜索函数来查阅该映射。
这个很长的页面如果涉及到这一点,Unicode Korean 可以帮助您。它有一个包含所有字符的表,该表表明字符的编号方式及其组成部分之间的某种结构化关系。
I'm no expert, but I think you're very unlikely to find a clean solution for what you want. There doesn't seem to be any relationship between a Korean character's Unicode value and the graphemes that it's made up of.
e.g. "이" is \uc774 and "ㅣ" is \u3163. From the perspective of the NSString, they're just two different characters with no specific relationship to each other.
I suspect that you will have to find or create an explicit mapping between characters and their graphemes, and then write your own search function that consults this mapping.
This very long page on Unicode Korean can help you, if it comes to that. It has a table of all the characters which suggests some structured relation between the way characters are numbered and their components.
如果将
compare:options
与 NSLiteralString 一起使用,它应该逐个字符进行比较,即 Unicode 代码点,而不考虑字形。compare:
的默认行为是不使用任何选项。您可以使用- decomposedStringWithCanonicalMapping
来获取输入字符串的 Unicode 字节,但我不确定它如何与compare:
交互。If you use
compare:options
with NSLiteralString, it should compare character by character, that is, the Unicode code points, regardless of the grapheme. The default behavior ofcompare:
is to use no options. You could use- decomposedStringWithCanonicalMapping
to get the Unicode bytes of the input string, but I'm not sure how that would interact withcompare:
.