我对这个映射器的东西很新,太混乱了。我有一个API请求,给定一个标题,API返回此:
{
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;
}
所以我为这个结果创建了一个可映射的类:
class SearchResponse: Mappable {
var isSuccess : String?
var searchArray: [Movie]?
var searchCount: String?
required init?(map: Map) {
}
func mapping(map: Map) {
isSuccess <- map["Response"]
searchArray <- map["Search"]
searchCount <- map["totalResults"]
}
}
class Movie: Mappable {
var posterURL : String?
var title : String?
var runtime : String?
var director : String?
var actors : String?
var genre : String?
var plot : String?
var production : String?
var year : String?
var imdbID : String?
var imdbRating : String?
required init?(map: Map) {
}
func mapping(map: Map) {
posterURL <- map["Poster"]
title <- map["Title"]
runtime <- map["Runtime"]
director <- map["Director"]
actors <- map["Actors"]
genre <- map["Genre"]
plot <- map["Plot"]
production <- map["Production"]
year <- map["Year"]
imdbID <- map["imdbID"]
imdbRating <- map["imdbRating"]
}
}
问题:我像这样映射了这个电影类,但是对于按标题搜索,我只有4个属性。但是对于下一次搜索,我需要所有这些搜索。是对的吗?或者我应该创建两个单独的类来处理每种响应?
确定!我在我的SearchTableViewController上显示了这个搜索的结果。现在我想要显示这部电影的更多细节(在此前一个响应中的任何“搜索”数组的电影)。为此,API提供了另一种类型的搜索,即通过imdbID进行搜索。所以我在我的SearchTableViewController上创建了一个segue来获取这个ID并传递给我的MovieViewController(将显示这些细节的视图):
let searchSegue = "segueFromSearch"
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let searchIndex = tableView.indexPathForSelectedRow?.row
let movie = movies?[searchIndex!]
let selectedImdbID = movie?.imdbID
print("|Table View Controler| Segue. IMDB_ID: \(String(describing: selectedImdbID))")
if segue.identifier == searchSegue {
if let destination = segue.destination as? MovieViewController {
destination.imdbID = selectedImdbID!
print("|Table View Controler| Inside of if let. Debug print: I get til here. imdbID = \(selectedImdbID!)")
}
}
}
我对此API请求的功能是:
//The movieSearched variable is the text typed on my searchBar
let URL = "https://www.omdbapi.com/?s=\(movieSearched)&type=movie"
Alamofire.request(URL).responseObject{ (response: DataResponse<SearchResponse>) in
print("response is: \(response)")
switch response.result {
case .success(let value):
let searchResponse = value
self.movies = (searchResponse.searchArray)
self.searchTableView.reloadData()
case .failure(let error):
let alert = UIAlertController(title: "Error", message: "Error 4xx / 5xx: \(error)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
好的,鉴于我对此的概述,让我们谈谈我的问题......
当我按ID搜索时,Json的回答是:
{
Actors = "Robbie Amell, Rachael Taylor, Shaun Benson, Gray Powell";
Awards = "N/A";
BoxOffice = "N/A";
Country = "USA, Canada";
DVD = "16 Sep 2016";
Director = "Tony Elliott";
Genre = "Sci-Fi, Thriller";
Language = English;
Metascore = "N/A";
Plot = "Trapped in a lab and stuck in a time loop, a disoriented couple fends off masked raiders while harboring a new energy source that could save humanity.";
Poster = "https://images-na.ssl-images-amazon.com/images/M/MV5BMjAxODQ2MzkyMV5BMl5BanBnXkFtZTgwNjU3MTE5OTE@._V1_SX300.jpg";
Production = Netflix;
Rated = "N/A";
Ratings = (
{
Source = "Internet Movie Database";
Value = "6.4/10";
},
{
Source = "Rotten Tomatoes";
Value = "60%";
}
);
Released = "16 Sep 2016";
Response = True;
Runtime = "88 min";
Title = ARQ;
Type = movie;
Website = "N/A";
Writer = "Tony Elliott";
Year = 2016;
imdbID = tt5640450;
imdbRating = "6.4";
imdbVotes = "17,481";
}
我通过ID做了这个alamofire请求搜索:
func getMovieById() {
let URL = "https://www.omdbapi.com/?i=\(String(describing: imdbID!)))"
Alamofire.request(URL).responseObject{ (response: DataResponse<SearchResponse>) in
print("|MovieController| Response is: \(response)")
let Result = response.result.value
print("Result for search by id: \(String(describing: Result!.searchArray))")
// Have to get the values here, right?
}
}
显然,我没有得到我想要的数据。所以......
问题:
我对这么多层感到困惑。另外,我是swift的初学者,我第一次使用这个ObjectMapper。很抱歉这里有很多代码,但我想我必须解释一下我的情况。
答案 0 :(得分:0)
您必须使用该属性的正确数据类型映射每个属性。响应中的一个对象包含布尔值值(例如&#34;响应&#34;),但您将其声明为字符串。我们必须准确匹配属性的数据类型,否则对象将是nil并且不会被映射。
按ID 搜索的回复与您的映射器类不匹配。
let Result = response.result.value
错了。 response.result.value
会产生SearchResponse
个对象。
底线
您必须首先获得映射部分。任何不匹配的类型都不会被映射。使用响应对象将显示所有映射而不是JSON的整个对象。所以它应该是:let movie = response.result.value
。然后,您可以访问电影的属性,例如ex. movie.actors