gpt4 book ai didi

ios - 使用 Alamofire (iOS) 从 OMDb API 获取数据

转载 作者:行者123 更新时间:2023-11-28 08:12:06 26 4
gpt4 key购买 nike

所以,我正在尝试创建一个 iOS 应用程序(我是初学者)在 IMDB 上搜索电影(使用 OMDb API 和 Alamofire)。语言是 swift 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 类(class)并陷入了我的 MovieDAO 类(class)(上面的代码):

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
}
}
}

我有一个带有搜索栏和表格 View 的表格 View Controller ,当用户键入电影标题时,我想在我的表格 View 中显示结果。

如何使我的搜索栏函数的结果成为我的 MovieDAO 将接收的变量? (对不起,如果我有什么不对的地方,请随时纠正我)

我的搜索栏测试获取用户文本:

func searchBar(_ searchBar: UISearchBar, textDidChange imdbTitle:String) {
print("Movie typed: \(imdbTitle)")
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

print("Movie searched: \(searchBar.text!)")
}

有任何方向、解释、教程指示吗?欢迎任何帮助!

最佳答案

使用 ObjectMapper 映射对象 https://github.com/Hearst-DD/ObjectMapper .并且还使用 AlamofireObjectMapper 检索数据 https://github.com/tristanhimmelman/AlamofireObjectMapper .您可以从这些链接中获取教程。

Alamofire.request(url,method: .yourmethod).validate().responseArray{ (response:DataResponse<[yourObjectMapperClass]>) 在

关于ios - 使用 Alamofire (iOS) 从 OMDb API 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43445546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com