gpt4 book ai didi

ios - 如何从NSUserDefaults检索数据?

转载 作者:行者123 更新时间:2023-12-03 02:30:45 25 4
gpt4 key购买 nike

目前,我尝试制作音乐播放列表应用程序。我尝试从NSUserDefaults保存和检索播放列表数据。保存数据时没有问题,但是检索数据时出现错误。

我有:

Terminating app due to uncaught exception 'MPMediaItemCollectionInitException', reason: '-init is not supported, use -initWithItems:



我正在关注 Play iPod playlist retrieved from a saved persistentid list的答案,但我尝试迅速编写它。

这是我的保存功能:
func savePlaylist(var mediaItemCollection: MPMediaItemCollection){

var items: NSArray = mediaItemCollection.items

var listToSave: NSMutableArray = NSMutableArray()

for song in items{

var persistentId: AnyObject! = song.valueForProperty(MPMediaItemPropertyPersistentID)

listToSave.addObject(persistentId)

}

var data: NSData = NSKeyedArchiver.archivedDataWithRootObject(listToSave)

NSUserDefaults.standardUserDefaults().setObject(data, forKey: "songsList")
NSUserDefaults.standardUserDefaults().synchronize()

}

这是我的检索功能:
func setupMusic(){

if NSUserDefaults.standardUserDefaults().objectForKey("songsList") != nil{

var theList: NSMutableArray = NSMutableArray()

var data: NSData = (NSUserDefaults.standardUserDefaults().objectForKey("songsList") as? NSData)!

var decodedData: NSArray = (NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSArray)!

theList.addObjectsFromArray(decodedData as [AnyObject])

var allTheSongs: NSMutableArray = NSMutableArray()

for var i = 0; i < theList.count; i++ {

var songQuery = MPMediaQuery.songsQuery()

var songs: NSArray = songQuery.items

allTheSongs.addObject(songs)

}

var currentQueue: MPMediaItemCollection = MPMediaItemCollection()

myMusicPlayer?.setQueueWithItemCollection(currentQueue)


}else{
println("fail!!!!!!!!!!!!")
}

}

我该如何解决?

最佳答案

这是问题var currentQueue: MPMediaItemCollection = MPMediaItemCollection()
您必须使用项目init
按照苹果的documentation

init(items:) Designated Initializer

Initializes a media item collection with an array of media items. Declaration

Swift

init!(items items: [AnyObject]!) Parameters items

The array of items you are assigning to the media item collection.



您必须使用以下项进行初始化
var currentQueue: MPMediaItemCollection = MPMediaItemCollection(items:itemsArray)

关于ios - 如何从NSUserDefaults检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30565812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com