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

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 = ["Mary", "Jose", "John", "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'm already 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 in advance for your attention :) Hugs:)

    
asked by anonymous 20.04.2017 / 17:19

1 answer

0

Try replacing the line

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

By:

item.lowercased().range(of: searchText.lowercased()) != nil
    
07.06.2017 / 14:52