我创建了一个用于向用户显示最新和顶级乡村歌曲的应用程序。我使用Firebase数据库发送和接收歌曲信息。这是在Xcode中使用Swift和UIKit。
我创建了一个名为Song的类,它处理从Firebase获得的所有信息。那个班级在下面。
import Foundation
import UIKit
class Song {
//private var _imageURL: String!
private var _videoURL: NSMutableURLRequest!
private var _songTitle: String!
private var _artistName: String!
private var _cellNum: Int!
private var _lastNum: Int!
private var _spotifyLink: String!
private var _itunesLink: String!
private var _lyrics: String!
private var _saved: Bool!
// var imageURL: String{
// return _imageURL
// }
var videoURL: NSMutableURLRequest{
return _videoURL
}
var songTitle: String{
return _songTitle
}
var artistName: String{
return _artistName
}
var cellNum: Int{
return _cellNum
}
var lastNum: Int{
return _lastNum
}
var spotifyLink: String{
return _spotifyLink
}
var itunesLink: String{
return _itunesLink
}
var lyrics: String{
return _lyrics
}
var saved: Bool{
get{
return _saved
}
set{
_saved = newValue
}
}
init(videoURL: NSMutableURLRequest, songTitle: String, artistName: String, cellNum: Int, lastNum: Int, spotifyLink: String, itunesLink: String, lyrics: String, saved: Bool) {
//_imageURL = imageURL
_videoURL = videoURL
_songTitle = songTitle
_artistName = artistName
_cellNum = cellNum
_lastNum = lastNum
_spotifyLink = spotifyLink
_itunesLink = itunesLink
_lyrics = lyrics
_saved = saved
}
}
在我的一个swift文件中,我初始化了一个类型为Song的新数组,以保存用户保存的信息。
var savedSongs = [Song]()
其中一个故事板中有一个按钮,如果用户点击它,它会将保存的歌曲添加到阵列中,当用户转到SavedVC故事板时,阵列中保存的所有项目都会显示为用户。单击按钮的功能如下所示。
@IBAction func save(_ sender: Any) {
if(saveBtn.currentTitle == "Save"){
song.saved = true
let p1 = song
savedSongs.append(p1)
//UserDefaults save here
saveBtn.setTitle("Unsave", for: .normal)
}
else if(saveBtn.currentTitle == "Unsave"){
if(savedSongs.count != 0){
//let items = (savedSongs.count) - 1
let title = song.songTitle
for save in 0...savedSongs.count-1{
let removeSave = savedSongs[save]
let checkTitle = removeSave.songTitle
if(checkTitle == title){
song.saved = false
savedSongs.remove(at: save)
//UserDefaults save here
saveBtn.setTitle("Save", for: .normal)
return
}
}
}
}
}
很明显,用户可以选择保存,之后,他可以重新点击按钮取消保存歌曲。在代码中,对象
song.(attribute)
被发送并且是所选单元格的表示,其中包含Song类中的信息。因为我现在有我的代码,一切正常。问题是当我离开应用程序时,数组中的内容不会停留。我尝试过UserDefaults,但我不知道如何使用我拥有的数组。
每次在上面的保存IBAction中单击按钮时,我想将数组保存为用户默认值。
答案 0 :(得分:0)
您需要对对象进行编码和解码,以便将它们保存到UserDefaults。
class Song: NSObject, NSCoding {
let name: String
let url: String
init(name: String, url: String) {
self.name = name
self.url = url
}
required init(coder decoder: NSCoder) {
self.name = decoder.decodeObject(forKey: "name") as? String ?? ""
self.url = decoder.decodeInteger(forKey: "url") as? String ?? ""
}
func encode(with coder: NSCoder) {
coder.encode(name, forKey: "name")
coder.encode(age, forKey: "url")
}
}
然后检索并保存:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// SAVE TO DEFAULTS
let song = Song(name: "Song", url: "http://song.com")
var songs = [Song]()
songs.append(song)
let encodedData = NSKeyedArchiver.archivedData(withRootObject: songs)
UserDefaults.standard.set(encodedData, forKey: "songs")
// RETRIEVE FROM DEFAULTS
if let data = UserDefaults.standard.data(forKey: "songs"),
let mySongs = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Song] {
mySongs.forEach({print( $0.name, $0.url)})
} else {
print("ERROR")
}
}
}