所以,我正在尝试创建一个在IMDB上搜索电影的iOS应用程序(我是初学者)(使用带有Alamofire的OMDb API)。语言很快3。
在阅读了很多教程之后,我做了两种连接到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>)
}
}
}
然后,我完成了我的Movie课程并陷入了我的MovieDAO课程(上面的代码):
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
}
}
}
我有一个带有搜索栏和表格视图的表格视图控制器,当用户输入我希望在我的表格视图中显示结果的电影标题时。
如何使搜索栏功能的结果成为我的MovieDAO将收到的变量? (对不起,如果我有些不对劲,请随时纠正我)
我的搜索栏测试以获取用户的文字:
func searchBar(_ searchBar: UISearchBar, textDidChange imdbTitle:String) {
print("Movie typed: \(imdbTitle)")
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("Movie searched: \(searchBar.text!)")
}
任何方向,解释,教程指示?欢迎提供每一个帮助!
答案 0 :(得分:0)
使用ObjectMapper映射对象https://github.com/Hearst-DD/ObjectMapper。并且还使用AlamofireObjectMapper来检索数据https://github.com/tristanhimmelman/AlamofireObjectMapper。您可以从这些链接中获取教程。
Alamofire.request(url,method:.yourmethod).validate()。responseArray {(response:DataResponse&lt; [yourObjectMapperClass]&gt;)in }