【发布时间】:2018-09-30 18:38:22
【问题描述】:
我能够从 api 获取 json。我已经填充了除imageView 之外的所有其他视图。我有一组图像,它们是字符串 url。我想下载这些图片。我目前正在使用SDWebImage。我需要将此数组转换为字符串,以便获取图像。我目前收到一条错误消息,因为 url 应该是字符串
无法转换类型“[图像]?”的值到预期的参数类型 '字符串'
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
}
}
【问题讨论】:
-
根据您之前的问题,如果
#text包含 url,请使用text: URL类型而不是String。如果你使用正确的类型,事情会变得更容易。
标签: ios json swift jsondecoder