I'm trying to create an iOS app (I'm a beginner) that search a movie on IMDB (using OMDb API with Alamofire). The language is swift 3.
After reading a lot of tutorials I did two methods to connect to the API:
func searchMoviesOnJson(imdbTitle: String, completionHandler: @escaping (Dictionary<String, Any>?) -> ()) {
let urlByName: String = "https://www.omdbapi.com/?s=\(imdbTitle)&type=movie"
//returns a list of movies that contains the title searched
Alamofire.request(urlByName).responseJSON {
response in
switch response.result {
case .success(let value):
let moviesJSON = value
completionHandler(moviesJSON as? Dictionary<String, Any>)
case .failure(_):
completionHandler(nil)
}
}
}
func getMovieFromJson(imdbID: String, completionHandler: @escaping (Dictionary<String, String>) -> ()) {
let urlById: String = "https://www.omdbapi.com/?i=\(imdbID)"
Alamofire.request(urlById).responseJSON {
response in
if let moviesJSON = response.result.value {
completionHandler(moviesJSON as! Dictionary<String, String>)
}
}
}
Then, I did my Movie
class and get stuck in my MovieDAO
class (code above):
class Movie {
let poster: String?
let title: String?
let runtime: String?
let genre: String?
let director: String?
let actors: String?
let plot: String?
let released: String?
let imdbID: String?
let imdbRating: String?
init(poster: String?, title: String?, runtime: String?, genre: String?, director: String?, actors: String?, plot: String?, released: String?, imdbID: String?, imdbRating: String?) {
//checking if is nil
if let isPoster = poster {
self.poster = isPoster
} else {
self.poster = nil
}
if let isTitle = title {
self.title = isTitle
} else {
self.title = nil
}
if let isGenre = genre {
self.genre = isGenre
} else {
self.genre = nil
}
if let isRuntime = runtime {
self.runtime = isRuntime
} else {
self.runtime = nil
}
if let isDirector = director {
self.director = isDirector
} else {
self.director = nil
}
if let isActors = actors {
self.actors = isActors
} else {
self.actors = nil
}
if let isPlot = plot {
self.plot = isPlot
} else {
self.plot = nil
}
if let isReleased = released {
self.released = isReleased
} else {
self.released = nil
}
if let isImdbID = imdbID {
self.imdbID = isImdbID
} else {
self.imdbID = nil
}
if let isImdbRating = imdbRating {
self.imdbRating = isImdbRating
} else {
self.imdbRating = nil
}
}
}
I have a table view controller with a search bar and a table view, when the user type the movie title I would like to show the results in my table view.
How can I make the result of my search bar functions be the variable that my MovieDAO
will receive? (Sorry if I sad something wrong, feel free to correct me, please)
My search bar test to get user's text:
func searchBar(_ searchBar: UISearchBar, textDidChange imdbTitle:String) {
print("Movie typed: \(imdbTitle)")
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("Movie searched: \(searchBar.text!)")
}
Any orientation, explanation, tutorial indication? Every help will be welcome!
Use ObjectMapper to map the objects https://github.com/Hearst-DD/ObjectMapper. And also use AlamofireObjectMapper to retrieve data https://github.com/tristanhimmelman/AlamofireObjectMapper .you can get the tutorials from these links.
Alamofire.request(url,method: .yourmethod).validate().responseArray{ (response:DataResponse<[yourObjectMapperClass]>) in }