简体   繁体   English

在Swift中的NSUserDefaults中保存自定义类对象

[英]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保存非NSDataNSStringNSNumberNSDateNSArrayNSDictionary类型的对象。

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 ,请使用NSKeyedArchiverNSKeyedUnarchiver来存储和检索您的自定义对象:

// 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.

相关问题 如何在Swift中归档和取消归档自定义对象? 或者如何在Swift中将自定义对象保存到NSUserDefaults? - How to archive and unarchive custom objects in Swift? Or how to save custom object to NSUserDefaults in Swift? 使用Swift将类中的struct保存到NSUserDefaults - Save struct in class to NSUserDefaults using Swift Swift:将一个对象数组和另一个对象数组保存到NSUserDefaults中 - Swift: Save an Array of Objects with another Array of Objects inside into NSUserDefaults 将字典保存到 NSUserDefaults Swift - Save Dictionary into NSUserDefaults Swift 如何使用NSCoding在Swift中使用NSUserDefaults将自定义对象保存到数组? - How to use NSCoding to Save Custom Object to Array with NSUserDefaults in Swift? 如何使用NSUserDefaults Swift存储对象的自定义数组 - How to store a custom array of objects using NSUserDefaults Swift 如何在数组中保存自定义对象并将其存储在NSUserDefaults - iPhone中 - How to save custom objects in array and store it in NSUserDefaults - iPhone 将自定义对象保存到NSUserDefaults - Save custom object to NSUserDefaults 在NSUserDefaults中存储自定义对象 - Store Custom Objects in NSUserDefaults 如何将自定义c ++类对象保存到NSUserDefaults? - How do I save a custom c++ class object to NSUserDefaults?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM