0
我正在从电话库中选择歌曲并获取歌曲路径的应用程序。我得到歌曲路径,并希望在Core Data中保存这首歌曲路径,但我将它保存在Core Data App Crashes中。请提出可能的解决方案。这是我的代码。保存核心数据应用程序崩溃中的歌曲url路径
@IBAction func selectMusic(_ sender: Any) {
mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker.allowsPickingMultipleItems = true
mediaPicker.delegate = self
self.present(mediaPicker, animated: true, completion: nil)
}
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
self.dismiss(animated: true, completion: nil)
let item: MPMediaItem? = (mediaItemCollection.items[0] as? MPMediaItem)
print(item)
let url: URL? = item?.value(forProperty: MPMediaItemPropertyAssetURL) as! URL?
print("My Url : \(url)")
var songsUrl : URL = url!
let urlString = songsUrl.absoluteString
songsUrlArray.append(urlString)
print("SONGS ARRAY \(songsUrlArray)")
audioPlayer = try! AVAudioPlayer(contentsOf: url!)
audioPlayer.play()
}
这里是我保存设置代码
@IBAction func saveConfiguration(_ sender: Any) {
// let email = defaults.value(forKey: "userEmail")
let email = "[email protected]"
let date = NSDate()
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd"
var dateString = dateFormatter.string(from: date as Date)
let appDel = UIApplication.shared.delegate as! AppDelegate
let context = appDel.persistentContainer.viewContext
let newItem = NSEntityDescription.insertNewObject(forEntityName: "Config_Table", into: context)
let itemTwo = NSEntityDescription.insertNewObject(forEntityName: "PlayList_Table", into: context)
newItem.setValue(email, forKey: "email")
newItem.setValue(configurationnameTextField.text, forKey: "config_name")
newItem.setValue(selectValueFromPickerView, forKey: "medi_type")
newItem.setValue(sliderSelectedValue, forKey: "medi_duration")
newItem.setValue(dateString, forKey: "medi_date")
itemTwo.setValue(configurationnameTextField.text, forKey: "config_name")
itemTwo.setValue(email, forKey: "email")
itemTwo.setValue(songsUrlArray, forKey: "song_path")
print(newItem)
print(itemTwo)
do {
try context.save()
print("Saved")
}
catch{
fatalError("Error")
}
}
这里是我的核心数据结构,其中song_path保存的URL。
这里是应急信息报告 enter image description here
什么是崩溃消息?在Simualtor/Device中安装后是否在CoreData中添加了song_path键?如果是,请安装新的副本或增加型号版本号。 – NeverHopeless
@NeverHopeless请在问题最后看到图片说明,我不明白你在说什么。 –
错误是明确的。你将你的song_path属性设置为一个String,但是当你创建你的实体时,你将放入一个Array中。这就是它崩溃的原因。 – Larme