将自定义对象保存到 NSUserDefaults
Save custom objects into NSUserDefaults
我有一条新闻 ViewController
和一条 TeamViewController
。 TeamViewController
包含一个 teamObjects 的 tableView,当被选中时被添加到数组中。我想将此数组添加到 NSUserDefaults
中,以便我可以从 NewsController
访问它们,其中包含需要 teamObjects 的 url 请求。但是我不断得到:
'Attempt to insert non-property list object (
""
) for key teams'
如果有比将其存储在 NSUserDefaults
中更好的方法,我愿意接受其他建议
didSelectRowAtIndexPath
方法
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let team = self.teamArray[indexPath.row] as Team
var removed = false
for (index, value) in enumerate(self.teamSelected) {
if (value == team) {
self.teamSelected.removeAtIndex(index)
removed = true
}
}
if (!removed) {
self.teamSelected.append(team)
}
var userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(self.teamSelected, forKey: "teams")
userDefaults.synchronize()
tableView.reloadData()
}
我的对象
class Team: NSObject{
var id: Int!
var name: NSString!
var shortname: NSString!
init(id: Int, name:NSString, shortname: NSString) {
self.id = id
self.name = name
self.shortname = shortname
}
}
你可以尝试 NSKeyedUnarchiver,如下所示
这里我在 UserDefault 中存储了 UIColor 对象 你可以用你的对象改变它
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];
并检索那个
NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
注意:这是Objective C但我希望你能在Swift
中隐藏它
希望这能解决您的问题
更新:Swift 5
存档在 Swift
do{
let colorAsData = try NSKeyedArchiver.archivedData(withRootObject: UIColor.red, requiringSecureCoding: true)
UserDefaults.standard.set(colorAsData, forKey: "myColor")
UserDefaults.standard.synchronize()
}catch (let error){
#if DEBUG
print("Failed to convert UIColor to Data : \(error.localizedDescription)")
#endif
}
在 Swift
取消归档
do{
if let colorAsData = UserDefaults.standard.object(forKey: "myColor") as? Data{
if let color = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [UIColor.self], from: colorAsData){
// Use Color
}
}
}catch (let error){
#if DEBUG
print("Failed to convert UIColor to Data : \(error.localizedDescription)")
#endif
}
实际上,您需要将自定义对象存档到 NSData
,然后将其保存到用户默认值并从用户默认值中检索并再次取消存档。
你可以这样存档
let teams = [Team(id: 1, name: "team1", shortname: "t1"), Team(id: 2, name: "team2", shortname: "t2")]
var userDefaults = UserDefaults.standard
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: teams)
userDefaults.set(encodedData, forKey: "teams")
userDefaults.synchronize()
然后像这样取消存档
let decoded = userDefaults.data(forKey: "teams")
let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Team]
但是如果你这样做你会得到
.Team encodeWithCoder:]: unrecognized selector sent to instance
你必须像这样让团队符合 NSCoding
class Team: NSObject, NSCoding {
var id: Int
var name: String
var shortname: String
init(id: Int, name: String, shortname: String) {
self.id = id
self.name = name
self.shortname = shortname
}
required convenience init(coder aDecoder: NSCoder) {
let id = aDecoder.decodeInteger(forKey: "id")
let name = aDecoder.decodeObject(forKey: "name") as! String
let shortname = aDecoder.decodeObject(forKey: "shortname") as! String
self.init(id: id, name: name, shortname: shortname)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(id, forKey: "id")
aCoder.encode(name, forKey: "name")
aCoder.encode(shortname, forKey: "shortname")
}
}
我有一条新闻 ViewController
和一条 TeamViewController
。 TeamViewController
包含一个 teamObjects 的 tableView,当被选中时被添加到数组中。我想将此数组添加到 NSUserDefaults
中,以便我可以从 NewsController
访问它们,其中包含需要 teamObjects 的 url 请求。但是我不断得到:
'Attempt to insert non-property list object ( "" ) for key teams'
如果有比将其存储在 NSUserDefaults
didSelectRowAtIndexPath
方法
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let team = self.teamArray[indexPath.row] as Team
var removed = false
for (index, value) in enumerate(self.teamSelected) {
if (value == team) {
self.teamSelected.removeAtIndex(index)
removed = true
}
}
if (!removed) {
self.teamSelected.append(team)
}
var userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(self.teamSelected, forKey: "teams")
userDefaults.synchronize()
tableView.reloadData()
}
我的对象
class Team: NSObject{
var id: Int!
var name: NSString!
var shortname: NSString!
init(id: Int, name:NSString, shortname: NSString) {
self.id = id
self.name = name
self.shortname = shortname
}
}
你可以尝试 NSKeyedUnarchiver,如下所示
这里我在 UserDefault 中存储了 UIColor 对象 你可以用你的对象改变它
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];
并检索那个
NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
注意:这是Objective C但我希望你能在Swift
中隐藏它希望这能解决您的问题
更新:Swift 5
存档在 Swift
do{
let colorAsData = try NSKeyedArchiver.archivedData(withRootObject: UIColor.red, requiringSecureCoding: true)
UserDefaults.standard.set(colorAsData, forKey: "myColor")
UserDefaults.standard.synchronize()
}catch (let error){
#if DEBUG
print("Failed to convert UIColor to Data : \(error.localizedDescription)")
#endif
}
在 Swift
取消归档do{
if let colorAsData = UserDefaults.standard.object(forKey: "myColor") as? Data{
if let color = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [UIColor.self], from: colorAsData){
// Use Color
}
}
}catch (let error){
#if DEBUG
print("Failed to convert UIColor to Data : \(error.localizedDescription)")
#endif
}
实际上,您需要将自定义对象存档到 NSData
,然后将其保存到用户默认值并从用户默认值中检索并再次取消存档。
你可以这样存档
let teams = [Team(id: 1, name: "team1", shortname: "t1"), Team(id: 2, name: "team2", shortname: "t2")]
var userDefaults = UserDefaults.standard
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: teams)
userDefaults.set(encodedData, forKey: "teams")
userDefaults.synchronize()
然后像这样取消存档
let decoded = userDefaults.data(forKey: "teams")
let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Team]
但是如果你这样做你会得到
.Team encodeWithCoder:]: unrecognized selector sent to instance
你必须像这样让团队符合 NSCoding
class Team: NSObject, NSCoding {
var id: Int
var name: String
var shortname: String
init(id: Int, name: String, shortname: String) {
self.id = id
self.name = name
self.shortname = shortname
}
required convenience init(coder aDecoder: NSCoder) {
let id = aDecoder.decodeInteger(forKey: "id")
let name = aDecoder.decodeObject(forKey: "name") as! String
let shortname = aDecoder.decodeObject(forKey: "shortname") as! String
self.init(id: id, name: name, shortname: shortname)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(id, forKey: "id")
aCoder.encode(name, forKey: "name")
aCoder.encode(shortname, forKey: "shortname")
}
}