编写下面的代码/保持警告Variable 'albumArt' was written to, but never read
Xcode生成的警告是一种更好的方法
在
func getCurrentlyPlayedInfo() {
DispatchQueue.main.async {
if let songInfo = self.mediaPlayer.nowPlayingItem {
self.songNameLabel.text = songInfo.title ?? ""
self.songAlbumLabel.text = songInfo.albumTitle ?? ""
self.songArtistLabel.text = songInfo.artist ?? ""
//This line generates the warning
if var albumArt = self.albumArtImageView?.image {
albumArt = songInfo.artwork?.image(at: CGSize(width: 400, height: 400)) ?? #imageLiteral(resourceName: "emptyArtworkImage")
//
}
}
}
对于上下文,此功能使用当前正在播放的歌曲“标题”,“专辑”,“艺术家”和“专辑插图”来更新应用的用户界面。
答案 0 :(得分:-1)
它之所以发出警告,是因为您为一个变量albumArt分配了一个值,下面的代码不再使用警告了:
func getCurrentlyPlayedInfo() {
DispatchQueue.main.async {
if let songInfo = self.mediaPlayer.nowPlayingItem {
self.songNameLabel.text = songInfo.title ?? ""
self.songAlbumLabel.text = songInfo.albumTitle ?? ""
self.songArtistLabel.text = songInfo.artist ?? ""
if let _ = self.albumArtImageView.image {
_ = songInfo.artwork?.image(at: CGSize(width: 400, height: 400)) ?? #imageLiteral(resourceName: "emptyArtworkImage")
//
}
}
}