user2640633
user2640633

Reputation:

Most appropriate syntax to silence warning and proper coding technique

What would be a better way of writing the following code/silence the warning Variable 'albumArt' was written to, but never read Xcode is generating

In

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")
      //

    }
  }
}

For context, this func is updating the app's UI with the currently playing song Title, Album, Artist and Album Artwork.

Upvotes: 0

Views: 68

Answers (1)

Manish_Nainwal
Manish_Nainwal

Reputation: 301

it was giving warning because you assigning a value to a variable albumArt which is not used further below is the warning less version of the above code:

    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")
      //

    }
      }
    }

Upvotes: -1

Related Questions