gpt4 book ai didi

ios - 如何访问数组中自定义对象的值?

转载 作者:行者123 更新时间:2023-11-30 12:42:00 24 4
gpt4 key购买 nike

我目前有一个自定义对象

public struct GenreData : Decodable {


public let id : NSNumber?
public let name : String


public init?(json: JSON) {

guard let name : String = "name" <~~ json
else {return nil}

self.id = "id" <~~ json
self.name = name

}
}

我有一个自定义对象的数组,并且正在尝试访问该对象的“id”部分,以便我可以将其插入另一个函数:

  var genreDataArray: [GenreData] = []
var posterStringArray: [String] = []
var posterImageArray: [UIImage] = []

GenreData.updateAllData(urlExtension:"list", completionHandler: { results in

guard let results = results else {
print("There was an error retrieving info")
return
}
self.genreDataArray = results

for _ in self.genreDataArray {

if let movieGenreID = self.genreDataArray[0].id {//This is where the ID is needed to access the posters

print(movieGenreID)

//Update posters based on genreID
GenrePosters.updateGenrePoster(genreID: movieGenreID, urlExtension: "movies", completionHandler: {posters in

//Must iterate through multiple arrays with many containing the same poster strings
for poster in posters {

//Check to see if array already has the current poster string, if it does continue, if not append to array
if self.posterStringArray.contains(poster){
continue
} else {
self.posterStringArray.append(poster)

//Use the poster string to download the corresponding poster
self.networkManager.downloadImage(imageExtension: "\(poster)",
{ (imageData) //imageData = Image data downloaded from web
in
if let image = UIImage(data: imageData as Data){
self.posterImageArray.append(image)

}
})
}
}
})
}
}
DispatchQueue.main.async {
self.genresTableView.reloadData()
}
})

到目前为止,它所做的就是访问 JSON 数据的第一个数组(有 19 个数组)并将第一个数组中的每个字符串附加到 posterStringArray,但我需要它遍历每个数组并仅附加如果海报字符串数组中尚不存在字符串。

最佳答案

我不能百分百确定我理解你的问题,但你不应该替换:

for _ in self.genreDataArray {

if let movieGenreID = self.genreDataArray[0].id

for item in self.genreDataArray {

if let movieGenreID = item.id

更新

要仅添加一项,请尝试在获取图像后添加中断:

          //Use the poster string to download the corresponding poster
self.networkManager.downloadImage(imageExtension: "\(poster)",
{ (imageData) //imageData = Image data downloaded from web
in
if let image = UIImage(data: imageData as Data){
self.posterImageArray.append(image)

}
})

break

关于ios - 如何访问数组中自定义对象的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42123266/

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