[英]Saving Songs url path in Core Data app Crashes
我正在開發一個應用程序,從電話庫中選擇歌曲並獲取歌曲路徑。 我得到了歌曲路徑,並希望在Core Data中保存這首歌曲路徑,我得到了歌曲路徑但是在Core Data App Crashes中保存時。 請提出可行的解決方案。 這是我的代碼。
@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 = "irfan@aol.com"
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。
這里是崩潰消息報告在這里輸入圖像描述
問題出在這里:
itemTwo.setValue(songsUrlArray, forKey: "song_path")
你的song_path
必須是NSString。 您正在嘗試保存陣列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.