gpt4 book ai didi

ios - AlamoFireObjectMapper 无法在对象数组中保存数据响应

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

我正在使用 AlamoFireObjectMapper 尝试将一些简单的 JSON 映射到一些类模型。似乎我能够正确检索 JSON 并打印它,但是当我尝试将我的响应附加到我的类类型的数组时,它不起作用。

示例 JSON: SampleJson

class MovieResults: Mappable {

var movieResults: [MovieInfo]?

required init?(map: Map) {
mapping(map: map)
}

func mapping(map: Map) {
// An array of objects
self.movieResults <- map["results"]
}
}

class MovieInfo: Mappable {

var movieTitle: String?
var movieID: Int?
var movieRating: String?
var movieReleaseDate: String?
var inTheaters: Bool?

var movieResults: [MovieInfo]?


required init?(map: Map) {

}

func mapping(map: Map) {
self.movieTitle <- map["title"]
self.movieID <- map["id"]
self.movieRating <- map["rating"]
self.movieReleaseDate <- map["release_date"]
self.inTheaters <- map["in_theaters"]
}
}

View Controller :

class resultViewController: UITableViewController {

var searchTerm: String?
var movieArray = [MovieInfo]()

override func viewDidLoad() {
super.viewDidLoad()
fetchAPI(search: searchTerm!)
}

func fetchAPI(search: String) {
let gboxAPI = "rK5M0dCTUd268hk121BpNpEAxMOmFuNh"
let URL = "http://api-public.guidebox.com/v1.43/US/\(gboxAPI)/search/movie/title/\(searchTerm!)/fuzzy"


Alamofire.request(URL).responseObject { ( response: DataResponse<MovieResults>) in

let result = response.result.value

if let movies = result?.movieResults {
for item in movies {
self.movieArray.append(item) //this doesn't work
print(item.movieTitle) //this works
}
}
}

/* This doesn't print anything
for some reason my array isn't truly being
popluated in the above fetchAPI call
*/
for item in movieArray {
print(item.movieTitle)
}

}
override func tableView(_ tablView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return movieArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell(style: .value1, reuseIdentifier: "UITableViewCell")

let movieItem = movieArray[indexPath.row]

cell.textLabel?.text = movieItem.movieTitle
cell.detailTextLabel?.text = movieItem.movieRating

return cell

}
}

我这辈子都想不通为什么我可以打印,但不能存储我的数据。这就是不合我意。

最佳答案

您可能无法创建 Mappable 类的对象以便向其附加数据。相反,您可以创建另一个模型类并使用它的对象。

class movieModel: NSObject {
var movieTitle: String?
var movieID: Int?
var movieRating: String?
var movieReleaseDate: String?
var inTheaters: Bool?
}

现在在 resultViewController 中创建这个类的对象,并用它来存储电影细节。

class resultViewController: UITableViewController {
let movieArray: [movieModel]? //create array of movieModel object
.......
........
if let movies = result?.movieResults {
for item in movies {
let movie = movieModel()//create object of movieModel
movie.movieTitle = item.movieTitle
movie.movieID = item.movieID
movie.movieRating = item.movieRating
movie.movieReleaseDate = item.movieReleaseDate
movie.inTheaters = item.inTheaters

self.movieArray.append(self.movie)
print(item.movieTitle)
}
}
........
}

关于ios - AlamoFireObjectMapper 无法在对象数组中保存数据响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40168476/

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