[英]Save custom class objects in NSUserDefaults in Swift
Im trying to save some objects in an array but my objects are used inherited properties But when trying to read the object it says 我试图将一些对象保存在数组中,但是我的对象使用了继承的属性,但是当尝试读取对象时,它说
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -decodeObjectForKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!'
The structure is 结构是
Service(SuperClass)
Service1(Subclass)
Service2
Service3
In this way im using this code 以这种方式即时通讯使用此代码
var S1:Service1 = Service1()
S1.apiKey = Apikey.text!
TableViewController.agregar(S1)
let datos = NSKeyedArchiver.archivedDataWithRootObject(TableViewController.Servicios!)
NSUserDefaults.standardUserDefaults().setObject(datos, forKey: "ServiciosGuardados")
NSUserDefaults.standardUserDefaults().synchronize()
And this is the classes functions 这是类函数
class Servicio:NSObject, NSCoding{
var nombre:String = "null"
var tipo:Int = 0
override init() {}
required init(coder aDecoder: NSCoder = NSCoder.empty()) {
self.nombre = aDecoder.decodeObjectForKey("nombre") as! String
self.tipo = aDecoder.decodeObjectForKey("tipo") as! Int
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(nombre, forKey: "nombre")
aCoder.encodeObject(tipo, forKey: "tipo")
}
-My problem is in here in Service1 which inherites from Servicio -我的问题是从Servicio继承的Service1中
class Service1: Servicio {
var apiKey = "null"
override init() {
super.init(coder: NSCoder())
}
required init(coder aDecoder: NSCoder) {
super.init(coder: NSCoder())
if let apiKey = aDecoder.decodeObjectForKey("apiKey") as? String {
self.apiKey = apiKey
}
}
override func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.apiKey, forKey: "apiKey")
}
You can't use NSUserDefaults
to save objects that are not of type NSData
, NSString
, NSNumber
, NSDate
, NSArray
or NSDictionary
. 您不能使用
NSUserDefaults
保存非NSData
, NSString
, NSNumber
, NSDate
, NSArray
或NSDictionary
类型的对象。
But you can serialize and save/load your custom objects in a file thanks to NSCoding
. 但是由于
NSCoding
您可以序列化和将自定义对象保存/加载到文件中。
Since your class already supports NSCoding
, use NSKeyedArchiver
and NSKeyedUnarchiver
to store and retrieve your custom objects: 由于您的类已经支持
NSCoding
,请使用NSKeyedArchiver
和NSKeyedUnarchiver
来存储和检索您的自定义对象:
// save your custom object in a file
NSKeyedArchiver.archiveRootObject(myCustomObject, toFile: "/path/to/filename")
// load your custom object from the file
if let temp = NSKeyedUnarchiver.unarchiveObjectWithFile("/path/to/filename") {
self.myCustomObject = temp
}
In this example, myCustomObject
could be a Service1
instance, or an array of instances, etc. 在此示例中,
myCustomObject
可以是Service1
实例或实例数组等。
Here is : 这是 :
Storable Types in NSUserDefaults
NSUserDefaults
存储类型
The NSUserDefaults
class acts very much like something called a Property List (aka plist). NSUserDefaults
类的行为非常类似于称为“属性列表”(aka plist)的行为。 It may be just a fancy interface for a plist, or it may be more, I'm not entirely sure. 它可能只是plist的一个精美界面,或者可能更多,我不确定。 Nonetheless, plists are limited in what kind of objects they can store.
但是,塑料存放者只能存储哪种物品受到限制。 The six types plists can store are:
可以存储的六种类型是:
NSData
NSString
NSNumber
NSDate
NSArray
NSDictionary
Here is obj-c example to store custom objects : How to store custom objects in NSUserDefaults 这是存储自定义对象的obj-c示例: 如何在NSUserDefaults中存储自定义对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.