我没有分配值。将“类别”识别为常量。我尝试过as吗?但是,当然,他告诉我“使用未声明的类型'category'
class FeedCell: UITableViewCell {
@IBOutlet weak var category: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var labelTitle: UILabel!
@IBOutlet weak var labelSubtitle: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
// assembled the cell
func updateCell(withFeedItem feedItem: RSSFeedItem) {
var imageLink = ""
var category = ""
for media in (feedItem.media!.mediaThumbnails)! {
imageLink = media.attributes?.url ?? "No image"
}
for categories in feedItem.categories! {
category = categories.value ?? "None"
guard let mediaThumbnails = feedItem.media?.mediaThumbnails,
let categories = feedItem.categories else { return }
for media in mediaThumbnails {
imageLink = media.attributes?.url ?? "No image"
}
for category in categories {
category = category.value ?? "None"
}
}
self.myImageView.sd_setImage(with: URL(string: imageLink), completed: nil)
self.labelTitle.text = feedItem.title
self.labelSubtitle.text = feedItem.description
self.category.text = category
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
应避免强行展开。在循环中,您正在强行展开feedItem.categories和feedItem.media?.mediaThumbnails。如果这些是可选的,则应该有一些理由。您应该首先使用guard或if let语句,这样可以确保它们不是nil,并且可以根据需要使用它们。您可以阅读here进一步了解如何进行安全展开。