我能够从 api 获取 json。我填充了除 imageView
之外的所有其他 View 。我有一组图像,它们是字符串 url。我想下载这些图片。我目前正在使用 SDWebImage
。我需要将这个数组转换成一个字符串,这样我才能获取图像。我目前收到一条错误消息,因为 url 应该是字符串
Cannot convert value of type '[Images]?' to expected argument type 'String'
import SDWebImage
class AlbumCell: UITableViewCell {
@IBOutlet weak var albumImageView: UIImageView!
var album: Album? {
didSet {
guard let url = URL(string: album?.image) else { return }
albumImageView.sd_setImage(with: url, completed: nil)
}
}
}
struct Album: Decodable {
let name: String
let image: [Images]
let artist: String
}
struct Images: Decodable {
let text: String
let size: String
enum CodingKeys: String, CodingKey {
case text = "#text"
case size
}
}
最佳答案
您的Album
结构包含一个属性image,它是Images
结构的数组。
在您的 URL(string: album?.image)
中,URL 构造函数需要一个字符串,但您提供的是一组图像。
您可以执行类似 URL(string: album?.image[0].text)
的操作以从图像数组中获取字符串。这将从图像数组中获取第一张图像,您可以根据需要更改此索引以获取其余图像。
关于ios - 如何将图像数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580962/