UISearchController无法识别umlaut,Swift,iOS

时间:2018-01-18 17:16:50

标签: ios swift unicode uisearchbar uisearchcontroller

我的应用程序中有搜索功能,但它无法识别任何变形金刚(ä,ö,ü)。我使用UISearchController。如何调整它识别变音符号的代码?

($0.title as AnyObject).contains(self.searchController.searchBar.text!.lowercased())

1 个答案:

答案 0 :(得分:1)

这对我来说很好。我会在lldb中检查你的字符串。

<ng-template dynamicComponent></ng-template>

如果您打算将字符串与变音符号匹配,那么您需要清理数据并从所有内容中删除变音符号。

E.g。

  func search() {
    let stringWithDiaeresis = "Reënter"
    let stringWithoutDiaeresis = "Reenter"
    let searchStringWithDiaeresis = "Reë"
    let searchStringWithoutDiaeresis = "Ree"

    if stringWithDiaeresis.contains(searchStringWithDiaeresis) {
      print("Range of Reë detected in Reënter")
    }

    if stringWithDiaeresis.contains(searchStringWithoutDiaeresis) {
      print("Range of Reë detected in Reenter")
    }

    if stringWithoutDiaeresis.contains(searchStringWithDiaeresis) {
      print("Range of Reë detected in Reenter")
    }

    if stringWithoutDiaeresis.contains(searchStringWithoutDiaeresis) {
      print("Range of Ree detected in Reenter")
    }

    /* Prints the following:
     Range of Reë detected in Reënter
     Range of Ree detected in Reenter
     */

    if (stringWithDiaeresis as AnyObject).contains(searchStringWithDiaeresis) {
      print("Reënter contains Reë")
    }

    if (stringWithDiaeresis as AnyObject).contains(searchStringWithoutDiaeresis) {
      print("Reënter contains Ree")
    }

    if (stringWithoutDiaeresis as AnyObject).contains(searchStringWithDiaeresis) {
      print("Reenter contains Reë")
    }

    if (stringWithoutDiaeresis as AnyObject).contains(searchStringWithoutDiaeresis) {
      print("Reenter contains Ree")
    }

    /* Prints the following:
     Reënter contains Reë
     Reenter contains Ree
     */
  }

这里的故事的寓意是你真的应该使用String类型可用的专用/更复杂的方法而不是泛型contains()。