Filtering Apostrophes (special characters) in UISearchBar (Swift)

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:
Code Block  
    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()
    }
}


What is data in your code ? Is it the whole table datasource ?

To understand your use case, data is an Array of String.
In the test case, data contains "Bob's". Exact ?

Then you search for Bob and you get Bob's Exact ?
But when you search for Bob', you don't get Bob's Exact ?

I tested the following in playground:
Code Block
let searchText = "Bob'"
let myData = ["Ok", "You", "Bob's"]
var filteredData: [String] = []
if searchText.isEmpty {
filteredData = myData
} else {
filteredData = myData.filter{$0.range(of: searchText, options: .caseInsensitive) != nil }
}
print(filteredData)

I get the correct result :
["Bob\'s"]

So the problem is not the filter.

How is data exactly defined ? What is its title property ?

You say:

as soon as I enter the apostrophe in the searchBar: Bob' the searchBar returns no results

What do you get exactly ? An empty table ?


Hi, thank you for the response.

Here is my: Struct, Array, and cellForRowAt func. As of now, when I search with an apostrophe an empty table is returned. Reading over your answer I believe the issue could be in the cellForRowAt func. Thanks.


Struct:
Code Block
struct Category {
    let title: String
    let items: [String]
}

Array:
Code Block
Category(title: "Bob's ", items: ["Data: xxxxx", "Data: xxxxx", "Data: xxxxx",]),


cellForRowAt Func:
Code Block     
    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
    }
}





You did not answer the last questions:

as soon as I enter the apostrophe in the searchBar: Bob' the searchBar returns no results

  • What do you get exactly ?

  • An empty table ?

In addition, could you show
Code Block
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { }


Could you show your tableView cellAtRow func ?

Could you also add a print statement in line 7

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

Hi thanks for the response,

To answer your question, I get a completely empty table. The cell (or any other cells) do not show up in the tableView.

numberOfRowsInSection func:
Code Block
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }



I have added a print statement and when I type an apostrophe the output that is returned is: filteredData []


Accepted Answer

as soon as I enter the apostrophe in the searchBar: Bob' the searchBar returns no results.

Are you sure the last character you entered is an apostrophe U+0027 (')?

Based on locale settings and used keyboard, iOS may use some other apostrophe-like character, for example right single quotation mark U+2019 ().

Please add this line at the top of searchBar(_:textDidChange:) and see what you get.
Code Block
        print(searchText.unicodeScalars.map{String($0.value, radix: 16)}.joined(separator: " "))


Also you may need to check if all the apostrophe-like characters in your Array are really U+0027.
Hi, thank you for the response.

I have resolved this issue. It turns out U+2019 (’) is the character that is the default "apostrophe" on the iPhone, and works with my UISearchBar. I honestly would have never been able to figure this out without your response.

Thanks,
Nicolas
Filtering Apostrophes (special characters) in UISearchBar (Swift)
 
 
Q