Swift MusicKitプログラミング その6

Swift MusicKitプログラミング その6

Limit

氷室京介をキーにした曲は沢山あるのに、5曲しか返ってこない!!

print("\nExample 2 --------------------")
var request2 = MusicCatalogSearchRequest(term: "氷室京介", types: [Song.self])
let response2 = try await request2.response()
let songs: MusicItemCollection<Song> = response2.songs
songs.forEach({
    print($0)
})
Example 2 --------------------
Song(id: "681932641", title: "KISS ME", artistName: "氷室京介")
Song(id: "681727272", title: "魂を抱いてくれ", artistName: "氷室京介")
Song(id: "681186219", title: "ANGEL", artistName: "氷室京介")
Song(id: "409098106", title: "B·Blue", artistName: "氷室京介")
Song(id: "681652218", title: "KISS ME", artistName: "氷室京介")

Limitのデフォルトは5です。
MusicKitのDocumentには書いていないです。
Apple Music APIのDocumentに書かれています。

Documentより

limit integer
The number of objects or number of objects in the specified relationship returned.
Default: 5
Maximum: 25

Limit指定

print("\nExample 2 --------------------")
var request2 = MusicCatalogSearchRequest(term: "氷室京介", types: [Song.self])
request2.limit = 25
let response2 = try await request2.response()
let songs: MusicItemCollection<Song> = response2.songs
songs.forEach({
    print($0)
})
Example 2 --------------------
Song(id: "681932641", title: "KISS ME", artistName: "氷室京介")
Song(id: "681727272", title: "魂を抱いてくれ", artistName: "氷室京介")
Song(id: "681186219", title: "ANGEL", artistName: "氷室京介")
Song(id: "409098106", title: "B·Blue", artistName: "氷室京介")
Song(id: "681134327", title: "LOVER'S DAY", artistName: "氷室京介")
Song(id: "681652218", title: "KISS ME", artistName: "氷室京介")
Song(id: "681606751", title: "SUMMER GAME", artistName: "氷室京介")
Song(id: "681932648", title: "CLOUDY HEART", artistName: "氷室京介")
Song(id: "409097673", title: "Claudia", artistName: "氷室京介")
Song(id: "1443135634", title: "CHU-RU-LU", artistName: "氷室京介")
Song(id: "409097112", title: "ダイヤモンド・ダスト", artistName: "氷室京介")
Song(id: "681625431", title: "JEALOUSYを眠らせて (RE-MIX VERSION)", artistName: "氷室京介")
Song(id: "681727269", title: "STAY", artistName: "氷室京介")
Song(id: "681727276", title: "SQUALL", artistName: "氷室京介")
Song(id: "681186220", title: "ROXY", artistName: "氷室京介")
Song(id: "681660093", title: "VIRGIN BEAT", artistName: "氷室京介")
Song(id: "681652220", title: "Memories Of Blue", artistName: "氷室京介")
Song(id: "993548761", title: "ANSWER (feat. 氷室京介)", artistName: "GLAY")
Song(id: "681186222", title: "DEAR ALGERNON", artistName: "氷室京介")
Song(id: "681932804", title: "Keep the faith", artistName: "氷室京介")
Song(id: "1524002394", title: "魂を抱いてくれ", artistName: "氷室京介")
Song(id: "681606778", title: "CALLING", artistName: "氷室京介")
Song(id: "1524002401", title: "Lover's Day", artistName: "氷室京介")
Song(id: "681932637", title: "Jealousyを眠らせて", artistName: "氷室京介")
Song(id: "314180454", title: "CALLING", artistName: "氷室京介")

Limit最大値より大きい指定

print("\nExample 3 --------------------")
var request3 = MusicCatalogSearchRequest(term: "氷室京介", types: [Song.self])
request3.limit = 100
let response3 = try await request3.response()
print(response3.debugDescription)

エラーになります。

Example 3 --------------------
2021-10-06 20:17:37.250315+0900 ExampleStudyMusicKit[2272:1149257] [DataRequesting] Failed to perform MusicDataRequest.Context(
  url: https://api.music.apple.com/v1/catalog/jp/search?term=%E6%B0%B7%E5%AE%A4%E4%BA%AC%E4%BB%8B&types=songs&omit%5Bresource%5D=autos&limit=100,
  currentRetryCounts: [.other: 1]
) with MusicDataRequest.Error(
  status: 400,
  code: 40005,
  title: "Invalid Parameter Value",
  detailText: "Value must be an integer less than or equal to 25",
  id: "",
  source: .parameter("limit")
  originalResponse: MusicDataResponse(
    data: 204 bytes,
    urlResponse: <NSHTTPURLResponse: 0x00000002831a9de0>
  )
).

では26以上はどうやって?は、次に。

Swift

Posted by shi-n