"Value for key 'root' was unexpected class 'NSArray'" 在 UserDefault 中检索对象数组时出错,Swift
"Value for key 'root' was unexpected class 'NSArray'" error when retrieve an array of object in UserDefault, Swift
我正在尝试检索 Swift 中的自定义对象数组,但出现错误
"UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'NSArray'. Allowed classes are '{(MusicCloud.Playlist)}"
我的项目是一个音乐播放器,所以我有一个 Song 对象和一个 Playlist 对象,它们将有一个名称并列出歌曲。
我已经对对象模型进行了编码:
class Playlist: NSObject, NSCoding{
var name: String
var songs: [Song]
var image: UIImage
override init() {
self.name = ""
self.songs = []
self.image = UIImage()
}
init(name: String, songs: [Song], image: UIImage) {
self.name = name
self.songs = songs
self.image = image
}
required convenience init(coder aDecoder: NSCoder) {
let name = aDecoder.decodeObject(forKey: "name") as! String
let songs = aDecoder.decodeObject(forKey: "songs") as! [Song]
let image = aDecoder.decodeObject(forKey: "photo") as! UIImage
self.init(name: name, songs: songs, image: image)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(songs, forKey: "songs")
aCoder.encode(image, forKey: "photo")
}
}
存储播放列表:
let playlist: Playlist = Playlist(name: txt.text!, songs: [], image: UIImage())
MusicService.shared.playlists.append(playlist)
do{
let encodedData: Data = try NSKeyedArchiver.archivedData(withRootObject: MusicService.shared.playlists, requiringSecureCoding: false)
self.userDefaults.set(encodedData, forKey: "playlist")
self.userDefaults.synchronize()
}catch let error{
print("error when add \(error)")
}
并检索它:
do{
let decodedPlaylists = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Playlist.self], from: decoded!) as! [Playlist]
print("here is decoded playlist: \(decodedPlaylists)")
MusicService.shared.playlists = decodedPlaylists
}catch let error{
print("error when retrieve \(error)")
}
完整的错误描述:
Domain=NSCocoaErrorDomain Code=4864 "value for key 'root' was of unexpected class 'NSArray'. Allowed classes are '{(
MusicCloud.Playlist
)}'."
如果我遗漏了什么,谁能告诉我。非常感谢!
我不知道为什么,但从
let decodedPlaylists = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Playlist.self], from: decoded!) as! [Playlist]
至
let decodedPlaylists = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(decoded!) as! [Playlist]
会为我解决这个问题。所以我会在这里关闭我的票。如果您注意到了,谢谢!
试试这个
let decodedPlaylists = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Playlist.self, NSArray.self], from: decoded!) as! [Playlist]
我猜,
以下解码代码包含NSArray
的数据结构
let songs = aDecoder.decodeObject(forKey: "songs") as! [Song]
我正在尝试检索 Swift 中的自定义对象数组,但出现错误
"UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'NSArray'. Allowed classes are '{(MusicCloud.Playlist)}"
我的项目是一个音乐播放器,所以我有一个 Song 对象和一个 Playlist 对象,它们将有一个名称并列出歌曲。
我已经对对象模型进行了编码:
class Playlist: NSObject, NSCoding{
var name: String
var songs: [Song]
var image: UIImage
override init() {
self.name = ""
self.songs = []
self.image = UIImage()
}
init(name: String, songs: [Song], image: UIImage) {
self.name = name
self.songs = songs
self.image = image
}
required convenience init(coder aDecoder: NSCoder) {
let name = aDecoder.decodeObject(forKey: "name") as! String
let songs = aDecoder.decodeObject(forKey: "songs") as! [Song]
let image = aDecoder.decodeObject(forKey: "photo") as! UIImage
self.init(name: name, songs: songs, image: image)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(songs, forKey: "songs")
aCoder.encode(image, forKey: "photo")
}
}
存储播放列表:
let playlist: Playlist = Playlist(name: txt.text!, songs: [], image: UIImage())
MusicService.shared.playlists.append(playlist)
do{
let encodedData: Data = try NSKeyedArchiver.archivedData(withRootObject: MusicService.shared.playlists, requiringSecureCoding: false)
self.userDefaults.set(encodedData, forKey: "playlist")
self.userDefaults.synchronize()
}catch let error{
print("error when add \(error)")
}
并检索它:
do{
let decodedPlaylists = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Playlist.self], from: decoded!) as! [Playlist]
print("here is decoded playlist: \(decodedPlaylists)")
MusicService.shared.playlists = decodedPlaylists
}catch let error{
print("error when retrieve \(error)")
}
完整的错误描述:
Domain=NSCocoaErrorDomain Code=4864 "value for key 'root' was of unexpected class 'NSArray'. Allowed classes are '{(
MusicCloud.Playlist
)}'."
如果我遗漏了什么,谁能告诉我。非常感谢!
我不知道为什么,但从
let decodedPlaylists = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Playlist.self], from: decoded!) as! [Playlist]
至
let decodedPlaylists = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(decoded!) as! [Playlist]
会为我解决这个问题。所以我会在这里关闭我的票。如果您注意到了,谢谢!
试试这个
let decodedPlaylists = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Playlist.self, NSArray.self], from: decoded!) as! [Playlist]
我猜,
以下解码代码包含NSArray
let songs = aDecoder.decodeObject(forKey: "songs") as! [Song]