简体   繁体   中英

get rid of + between words while searching

I'm doing an iOS application where the user insert a movie title and the application tells the user the plot and actor and display an image for the movie.
The database is http://www.omdbapi.com/
The application works fine but with movies with 2 words i must insert a + to make it work (is there a way to fix that..)

OMDB is a class with parameters actors and plot and image

public func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
    let searchtext=searchBar.text
           getData(withTitle: searchtext!)
            {

                (result: String?, data: Omdb?, error: String?) in
                if result == "Success"
                {
                    self.tempOmdb = data!
                    self.tableview.reloadData()

                                         }
                else{
                    print(error)
                }
            }

}

All you need to do is replace the spaces with plusses.

let modifiedSearchText = searchtext.replacingOccurrences(of: " ", with: "+")

So here is your updated code (I also cleaned it up a little):

public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    guard let searchtext = searchBar.text else { return }
    let modifiedSearchText = searchtext.replacingOccurrences(of: " ", with: "+")
    getData(withTitle: modifiedSearchText) { (result: String?, data: Omdb?, error: String?) in
        if result == "Success" {
            self.tempOmdb = data!
            self.tableview.reloadData()
        }
        else {
            print(error)
        }
    }
}

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