How to use the Search Bar with the person class - Swift 3

Asked

Viewed 76 times

0

Good morning. I have the class person

class pessoa
{
    var nome : String = “”;
    var idade : Int = 0;
    var foto  : UIImage!;    
}
extension pessoa
{
   class func Gerar_Registros(pQtd : Int) -> Array<pessoa>
   {
       var vpessoa : pessoa;
       for A in 1...pQtd
       {
          vpessoa = pessoa();
          vpessoa.nome = “Nome..: \(A)”;
          vpessoa.idade = A;
          vpessoa.foto =  UIImage(named : “foto1”);    
       }
   }
}

Normally to make the search bar work I use this variable

var Array_name = ["Maria","Jose","João","Antonio"],

var Array_search : [String]!;

> Class vc_01 : UIViewController
>         {
>            var Array_Nome = [“Maria”,”Jose”,”João”,”Antonio”];
>            var Array_Busca : [String]!;
>            override func viewDidLoad()
>             {
>                 super.viewDidLoad()
>                 
>                 Array_Busca = Array_Nome;    
>             }    
> 
> func tableView(_ tableView : UITableView, numberOfRowsInSection
> Numeros_Linhas_na_Secao : Int)-> Int // Esta Metodo faz parte do
> UITableViewDataSource
>     {
>         return self.Array_Busca.count;
>     }
>     
>     
>     
>     
>     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
>         
>         let cell = tableView.dequeueReusableCell(withIdentifier: "cel01") as! cel01
>         
>         cell.ContactNameLabel.text! = Array_Busca[indexPath.row]
>         
>         cell.selectionStyle = .none
>         
>         return cell
>         
>     }
> 
>     func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
>     {
>             Array_Busca = searchText.isEmpty ? Array_Nome : Array_Nome.filter { (item: String) -> Bool in
>       
>             return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
>         }
>         
>         tableView.reloadData()
>     }
>     
>     func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
>         self.searchBar.showsCancelButton = true
>     }
>     
>     
>     func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
>         searchBar.showsCancelButton = false
>         searchBar.text = ""
>         searchBar.resignFirstResponder()
>     }
>         }

I’ve been trying for several days to make the above code run with the person class. Does anyone know how to do ??? Could you tell me how?? Thanks for your attention :) Hugs :)

1 answer

0

Try to replace the line

return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil

For:

item.lowercased().range(of: searchText.lowercased()) != nil
  • Good morning Felipe... So... the original ITEM is a STRING and what I would like to know is how to search with a class... What would the line look like: Return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil .... For the PESSOA class?? What would I have to put in place of the item?? Hugs and thanks for your attention :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.