我有一个基于结构的数组,该结构包括歌曲标题和歌手作为字符串,其图稿为MPMediaItemArtwork
类型。要使预览在Swift
UI中工作,请参阅
struct TrackRow_Previews: PreviewProvider {
static var previews: some View {
TrackRow(track: Track(name: "Track 1", artist: "Artist 1", artwork: [MPMediaItemArtwork]))
}
}
当我注释掉预览时,视图工作正常,因此,我能够将选定的MPMediaItemArtwork
(即MPConcreteMediaItemArtwork: 0x281efb890
)打印到控制台。但它也不接受。我可以为预览图稿添加什么值以使预览提供程序起作用?
其他详细信息:
struct Track: Hashable {
var name: String
var artist: String
var artwork: MPMediaItemArtwork
}
func getTracks(chosenPlaylist: String) {
trackArray = []
for playlist in playlists! {
if (playlist.value(forProperty: MPMediaPlaylistPropertyName)as! String) == chosenPlaylist {
let songs = playlist.items
for song in songs {
if song.artwork != nil {
trackArray.append(Track(name: (song.value(forProperty: MPMediaItemPropertyTitle) as! String), artist:(song.value(forProperty: MPMediaItemPropertyArtist) as! String), artwork: song.artwork!))
}
}
sortArray()
}
}
}
答案 0 :(得分:1)
我的问题是,我没有导入MediaPlayer,因为我不需要导入MediaPlayer,但显然需要预览视图。起作用的代码如下:
struct TrackRow_Previews: PreviewProvider {
static var previews: some View {
TrackRow(track: Track(name: "Track 1", artist: "Artist 1", artwork:MPMediaItemArtwork(image: UIImage(imageLiteralResourceName: "Artwork"))))
}
}
答案 1 :(得分:0)
我将假设artwork
是MPMediaItemArtwork
的数组,因此对于预览,我将仅抓取数组中的第一项,如下所示:
struct MPMediaItemArtwork {
var name: String
}
struct Track {
var name: String
var artist: String
var artwork: [MPMediaItemArtwork]
}
struct ContentView_Previews: PreviewProvider {
static var artwork = MPMediaItemArtwork(name: "0x281efb890")
static var mediaArray: [MPMediaItemArtwork] = [artwork]
static var previews: some View {
TrackRow(track: Track(name: "Track 1", artist: "Artist 1", artwork: [mediaArray[0]]))
}
}