cannot subscript a value of type [MPMediaItem]

送分小仙女□ 提交于 2019-12-24 15:20:05

问题


I am trying to execute this code in order to get all the music playlists that the users can have on their phones.

    var queryPlaylists = MPMediaQuery.playlistsQuery()
    print(queryPlaylists.collections[0])

However, when I try to access any of the elements of queryPlaylists.collections I get a compilation error:

Cannot subscript a value of type [MPMediaItem].

This does not make any sense to me since the above in an array. In addition this used to work before xcode 7.0.


回答1:


I figured out in order to access the elements in the queryPlaylists you need to create a new variable and cast it to [MPMediaPlaylist] as

var playlists = queryPlaylists.collections as! [MPMediaPlaylist]

now you can access the elements of playlists



来源:https://stackoverflow.com/questions/32799861/cannot-subscript-a-value-of-type-mpmediaitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!