skip to Main Content

I currently have a UISearchBar that works fine, however, I’ve noticed that when entering apostrophes in the searchBar it does not return any results. For example: If I have a String: Bob’s if I search Bob it returns said string, however, as soon as I enter the apostrophe in the searchBar: Bob’ the searchBar returns no results.

I’ve searched online for solutions, however, have not found anything that works. Any response would be greatly appreciated, thanks.

UISearchBar Code:

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        filteredData = data
    } else {
        filteredData = data.filter{$0.title.range(of: searchText, options: .caseInsensitive) != nil }
    }
    self.tableView.reloadData()
}

}

Struct:

struct Category {
    let title: String

    let items: [String]
}

Array:
Category(title: "Bob's ", items: ["Data: xxx", "Data: xxx", "Data: xxx",]),

cellForRowAt Func:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

        let title = filteredData[indexPath.row].title

          cell.myLabel.text = title

          cell.myImg.image = UIImage(named: title + ".png")

          return cell

    }

}

Output returned in console when apostrophe is typed in searchBar:
filteredData = []

2

Answers


  1. Chosen as BEST ANSWER

    This has been resolved. It turns out there are variations of apostrophes. There is the U+0027 (') and the U+2019 (’). The U+2019 (’) is the default apostrophe on the iPhone and works with the UISearchBar code above. Hopefully, this helps anyone who has the same issue as me.


  2. I was able to resolve the same issue by changing the Text input traits in Storyboard for the UISearchBar as below. Hope this helps

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search