gpt4 book ai didi

ios - 在 swift ( ios ) 中将字典转换为对象

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

我有这个函数 searchMoviesOnJson:

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

这给了我来自 api 的响应(例如:imdbTitle = "arq"):

{
Response = True;
Search = (
{
Poster = "https://images-na.ssl-images-amazon.com/images/M/MV5BMjAxODQ2MzkyMV5BMl5BanBnXkFtZTgwNjU3MTE5OTE@._V1_SX300.jpg";
Title = ARQ;
Type = movie;
Year = 2016;
imdbID = tt5640450;
},
{
Poster = "N/A";
Title = Arq;
Type = movie;
Year = 2011;
imdbID = tt2141601;
},
{
Poster = "N/A";
Title = "A.R.Q.";
Type = movie;
Year = 2015;
imdbID = tt3829612;
}
);
totalResults = 3;
}

所以我创建了一个这个类 MovieByTitle

class MovieByTitle {

var poster : String?
var title : String?
var year : Int?
var imdbID : String?

init(poster: String?, title: String?, year: Int?, imdbID: String?) {

//validation

if let isPoster = poster { self.poster = isPoster }
else { self.poster = nil }

if let isTitle = title { self.title = isTitle }
else { self.title = nil }

if let isYear = year { self.year = isYear }
else { self.year = nil }

if let isImdbID = imdbID { self.imdbID = isImdbID }
else { self.imdbID = nil }

}
}

现在我怀疑,我还创建了这个 MovieDAO:

类 MovieDao {

func getMovies(imdbTitle: String, completionHandler: @escaping (([MovieByTitle]) -> ())) {

//function that conects to the api
searchMoviesOnJson(imdbTitle: imdbTitle, completionHandler: {
moviesJSON in

//array to keep the attributes received by the dictionary
var moviesArray = [MovieByTitle]()

//searchResults is the response from my request as an array
if let searchResults = moviesJSON?["Search"] as? NSArray{

for searchResult in searchResults {

let movieResult = searchResult as! Dictionary<String,Any>
let movieDetail = MovieByTitle()

movieDetail.poster = movieResult["Poster"] as? String
movieDetail.title = movieResult["Title"] as? String
movieDetail.year = movieResult["Year"] as? Int
movieDetail.imdbID = movieResult["imdbID"] as? String

moviesArray.append(movieDetail)

}
}
})
}
}

但是 xcode 返回了一行错误: 让 movieDetail = MovieByTitle()

错误信息:在调用中缺少参数'poster'的参数(等等)

正确的语法是什么?将我的字典响应转换为对象的更好方法是什么?

最佳答案

您的 MovieByTitle 初始化函数需要 4 个缺少的参数。

解决方案 1:添加辅助 init:

init() {}

解决方案 2:通过为它们提供默认值来将现有的初始化参数定义为可选:

init(poster: String? = nil, title: String? = nil, year: Int? = nil, imdbID: String? = nil)

方案三:调用已有的init,并传入它需要的参数:

let movieDetail = MovieByTitle(poster: movieResult["Poster"] as? String, title: movieResult["Title"] as? String, year: movieResult["Year"] as? Int, imdbID: movieResult["imdbID"] as? String)

关于ios - 在 swift ( ios ) 中将字典转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458815/

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