Check if song is already playing

How would I check if a song, upon being clicked on didSelectItem, is playing? And how would I make sure, the program does not restart the song that is playing upon being clicked on?


You’ll have to save whenever a song is selected to be played, maybe in a UserDefault?

And then when a user selects a different song, check against the UserDefault and see if the song is already playing

1 Like

Thanks for the response Mikaela.

I have actually solved this problem, I had to make my Song struct conform to an equatable protocol, which lets us compare two instances, so in my case, the song that has just been clicked in comparison to the song currently playing

import Foundation

  struct Song: Equatable {

     let name: String
     let imageName: String
     let trackName: String
     static func == (lhs: Self, rhs: Self) -> Bool {
     return lhs.trackName == rhs.trackName