简体   繁体   中英

SWIFT error creating SearchBar

I'm new on SWIFT programming, and as usual i have a problem, maybe it's nothing, but I'll be glad if u can help me understanding where I wrong. Sorry for the italian variables name, but this leaned to be a try so I prefer to do in italian....

import UIKit

class ListaTableViewController: UITableViewController {
var listaSpesa = [Alimento]()
var resultSearchController = UISearchController()   //SearchBar

override func viewDidLoad() {
    super.viewDidLoad()
    //inizializzazione di una lista con l'aggiunta di elementi alla lista
    self.listaSpesa = [
        Alimento(categoria: ["Dolci"], nome: "cioccolato"),
        Alimento(categoria: ["Dolci"], nome: "barrette energetiche"),
        Alimento(categoria: ["Dolci", "Altro"], nome: "caramelle"),
        Alimento(categoria: ["Carne","Dolci"], nome: "cotolette"),
        Alimento(categoria: ["Carne"], nome: "polpette"),
        Alimento(categoria: ["Altro"], nome: "dentifricio"),
        Alimento(categoria: ["Altro"], nome: "carta alluminio")
    ]

    //aggiorna la tabella ricaricando le funzioni
    self.tableView.reloadData()


    //Aggiungo la SearchBar
    self.resultSearchController = ({
        // creo un oggetto di tipo UISearchController
        let controller = UISearchController(searchResultsController: nil)

        // il searchResultsUpdater, ovvero colui che gestirà gli eventi di ricerca, sarà la ListaTableViewController (o self)
        controller.searchResultsUpdater = self

        // rimuove la tableView di sottofondo in modo da poter successivamente visualizzare gli elementi cercati
        controller.dimsBackgroundDuringPresentation = false

        // impongo alla searchBar, contenuta all'interno del controller, di adattarsi alle dimensioni dell'applicazioni
        controller.searchBar.sizeToFit()

        // atacco alla parte superiore della TableView la searchBar (la visualizzo)
        self.tableView.tableHeaderView = controller.searchBar

        // restituisco il controller creato
        return controller
    })()
}
}

this is the part where i have problems, please, help me, everywhere i went says the same things that i've done... This is the string where I have the error

controller.searchResultsUpdater = self

And this is the error:

(Cannot assign a value of type 'ListaTableViewController' to a value of type 'UISearchResultUpdating?')

PS sorry for my english ^^'

UISearchResultUpdating is a protocol. You need to tell the compiler that you intend to implement that, by putting it in the line where you declare your class,

class ListaTableViewController: UITableViewController, UISearchResultUpdating

You will get another error if you don't actually implement the required methods in that protocol.

After declaring UISearchResultUpdating as mentioned:

class ListaTableViewController: UITableViewController, UISearchResultUpdating

The error exists as long as one does not implement the required function.

func updateSearchResultsForSearchController(searchController: UISearchController)

It´s simply a confusing error message ;-)
"ListaTableViewController does not confirm to protocol UISearchResultUpdating" would be more helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM