[英]swift - How to make Add to Favorites with NSURL array
I'm trying to build an app stores specific sounds which user can play. 我正在尝试构建一个应用程序存储用户可以播放的特定声音。 I assure you I've searched for 2 days but I couldn't what I look for.
我向你保证我已经搜索了2天,但我找不到我想要的东西。 (Or I couldn't successful with those tuts idk)
(或者我不能用那些tuts idk成功)
The app stores sounds like this: 应用程序商店的声音如下:
var sound1 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "Adios Amigos", ofType: "mp3")!)
var sound2 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "ses2", ofType: "mp3")!)
var sound3 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "ses3", ofType: "mp3")!)
var sound4 = NSURL(fileURLWithPath: Bundle.main.path(forResource: "ses4", ofType: "mp3")!)
var soundArray: [NSURL] = [sound1, sound2, sound3, sound4]
here is my button that play sounds as random: 这是我的按钮,播放声音随机:
@IBAction func Rastgele(sender: Any) {
let randNo = Int(arc4random_uniform(UInt32(soundArray.count))) // 0...ArrayCount
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOf: (soundArray[randNo] as NSURL) as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
self.sesLabel.text = soundArray[randNo].deletingPathExtension?.lastPathComponent
soundIndex = randNo} catch {
print(error)
}
}
and here is my IBAction button that I couldn't fill inside with code. 这是我的IBAction按钮,我无法填写代码。
@IBAction func Favori(_ sender: Any) {
// empty
}
I tried to use NSUserdefaults but I couldn't achieve what I want. 我试图使用NSUserdefaults,但我无法实现我想要的。 I also tried to pull the array item from my array to insert another array but I faced many problems with because of type of array(NSURL)
我也试图从我的数组中拉出数组项来插入另一个数组但由于数组类型(NSURL)我遇到了很多问题
So, 'Favori' button should store as Favorites then I can show favorited sounds in other table view. 因此,'Favori'按钮应存储为收藏夹,然后我可以在其他表视图中显示收藏的声音。
Thanks in advance. 提前致谢。
EDIT 编辑
I found a solution using Core Data and converting NSURLs to URL. 我找到了一个使用Core Data并将NSURL转换为URL的解决方案。 Thanks to the @rmaddy for helpful comment.
感谢@rmaddy的有用评论。 The working code is down below;
工作代码低于;
@IBAction func Favori(_ sender: Any) {
// save core data
let app = UIApplication.shared.delegate as! AppDelegate
let context = app.persistentContainer.viewContext
let newSound = NSEntityDescription.entity(forEntityName: "Sounds", in: context)
let sound = NSManagedObject(entity: newSound!, insertInto: context)
sound.setValue(soundArray[soundIndex].deletingPathExtension().lastPathComponent, forKey: "soundName")
do { try context.save()
soundItems.append(sound)
print(sound)
}
catch
{
print("error")
}
}
When you click on the button, save the URL into core data or a local data base using sqlite or a server. 单击该按钮时,使用sqlite或服务器将URL保存到核心数据或本地数据库中 。 If you just want to pass it to another tableview without saving it (data will lost when you kill the app), you can pass that around using a delegate or notification center .
如果您只想将其传递给另一个tableview而不保存它(当您终止应用程序时数据将丢失),您可以使用委托或通知中心传递它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.