如何使用 UserDefaults 保存包含来自已创建 class 的信息的数组
How to save an array that contains information from a created class using UserDefaults
我创建了一个应用程序,用于向用户展示最新和最热门的乡村歌曲。我使用 Firebase 数据库发送和接收歌曲信息。这是在 Xcode 中使用 Swift 和 UIKit。
我创建了一个名为 Song 的 class 来处理我从 Firebase 获得的所有信息。 class在下面。
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)
已发送,是所选单元格的表示,其中包含歌曲 class 中的信息。因为我现在有我的代码,所以一切正常。问题是当我离开应用程序时,数组中的内容不会保留。我已经尝试过 UserDefaults,但我不确定如何使用我拥有的数组来做到这一点。
每次在上面的保存 IBAction 中单击按钮时,我都想将数组保存为用户默认值。
您需要对对象进行编码和解码才能将它们保存到 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( [=11=].name, [=11=].url)})
} else {
print("ERROR")
}
}
}
我创建了一个应用程序,用于向用户展示最新和最热门的乡村歌曲。我使用 Firebase 数据库发送和接收歌曲信息。这是在 Xcode 中使用 Swift 和 UIKit。
我创建了一个名为 Song 的 class 来处理我从 Firebase 获得的所有信息。 class在下面。
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)
已发送,是所选单元格的表示,其中包含歌曲 class 中的信息。因为我现在有我的代码,所以一切正常。问题是当我离开应用程序时,数组中的内容不会保留。我已经尝试过 UserDefaults,但我不确定如何使用我拥有的数组来做到这一点。
每次在上面的保存 IBAction 中单击按钮时,我都想将数组保存为用户默认值。
您需要对对象进行编码和解码才能将它们保存到 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( [=11=].name, [=11=].url)})
} else {
print("ERROR")
}
}
}