MusicKit: How to search for a single song by ID

How can I search for a single song by using its song ID? I tried the following code but it's not working:

MusicCatalogResourceRequest(matching: Song.self, equalTo: song.id.rawValue)

I get the following errors in Xcode:

  • Cannot convert value of type 'Song.Type' to expected argument type 'KeyPath<MusicItemType.FilterType, String>'
  • Generic parameter 'MusicItemType' could not be inferred

I have the song ID saved as a string inside song so I just want to grab the full MusicKit MusicItemType from the API. I looked at the documentation but it doesn't make any sense to me.

Please help!

Answered by FPST in 803761022

You need to infer a type manually and tell which property should match yours.

MusicCatalogResourceRequest<Song>(matching: \.id, equalTo: song.id.rawValue)

This is what you are looking for.

Apart from that I strongly advise you to switch using IRSC (or UPC for albums) instead of the song id. The song id (or album id) can change at any time. Read the explanation here

You need to infer a type manually and tell which property should match yours.

MusicCatalogResourceRequest<Song>(matching: \.id, equalTo: song.id.rawValue)

This is what you are looking for.

Apart from that I strongly advise you to switch using IRSC (or UPC for albums) instead of the song id. The song id (or album id) can change at any time. Read the explanation here

MusicKit: How to search for a single song by ID
 
 
Q