iOS swift 简单的对象存储和读取

第一步:定义一个对象,并且把它序列化。
class Person:NSObject,NSCoding
{
    override init()
    {
        
    }
    var name = "zhang"
    var ega:Int32 = 22
    
    required init(coder aDecoder: NSCoder){
        
        super.init()
        
        self.name = aDecoder.decodeObjectForKey("name") as! String
        self.ega = aDecoder.decodeInt32ForKey("ega")
        
    }
    
    func encodeWithCoder(aCoder: NSCoder)
    {
        aCoder.encodeObject(self.name, forKey: "name")
        aCoder.encodeInt32(self.ega, forKey: "ega") 
    }

}

对象格式定义好,就可以把它储存了。

方法一 :使用NSUserDefaults
    func keepNSUserDefaultsLocal()
    {
        
        var vperson = Person()
        vperson.ega = 55
        vperson.name = "李四"
        var personData = NSKeyedArchiver.archivedDataWithRootObject(vperson)
        NSUserDefaults.standardUserDefaults().setObject(personData, forKey: "person1")
        
        
        //读取
        /**
        
        //当第一次使用,要设置默认值可以采用
        NSUserDefaults.standardUserDefaults().registerDefaults(["key1":"222","key2":55])
        */
        
        var personData2 = NSUserDefaults.standardUserDefaults().objectForKey("person1") as! NSData
        var person = NSKeyedUnarchiver.unarchiveObjectWithData(personData2) as! Person
        
        
    }


方法二:把对象储存在本地文件

    func keepLocalFile()
    {
        
        var vperson = Person()
        vperson.ega = 45
        vperson.name = "王五"
        
        var pathStr = NSHomeDirectory().stringByAppendingPathComponent("person.data")
        
        NSKeyedArchiver.archiveRootObject(vperson, toFile: pathStr)
        
        //读出
        var personNew = NSKeyedUnarchiver.unarchiveObjectWithFile(pathStr) as! Person
        
        
    }

方法三:多个对象直接存储并且分别读出单个值。
func muchPersonLocal()
    {
        var person1 = Person()
        person1.ega = 51
        person1.name = "李四"
        
        var person2 = Person()
        person2.ega = 52
        person2.name = "王五"
        
        var person3 = Person()
        person3.ega = 53
        person3.name = "赵六"
        
        var data = NSMutableData()
        var archive = NSKeyedArchiver(forWritingWithMutableData: data)
        
        archive.encodeObject(person1, forKey: "lisi")
        archive.encodeObject(person2, forKey: "wangwu")
        archive.encodeObject(person3, forKey: "zhaoliu")
        archive.finishEncoding()
        var  writePath = NSHomeDirectory().stringByAppendingPathComponent("muchperson.data")
        data.writeToFile(writePath, atomically: true)
        
        
        //分别读出数据
        
        var readData = NSData(contentsOfFile: writePath)
        
        var unarchiver = NSKeyedUnarchiver(forReadingWithData: readData!)
        
        var Lisi = unarchiver.decodeObjectForKey("lisi") as! Person
        var wangwu = unarchiver.decodeObjectForKey("wangwu") as! Person
        var zhaoliu = unarchiver.decodeObjectForKey("zhaoliu") as! Person
  
        print(Lisi.name)
        print(wangwu.name)
        print(zhaoliu.name)

    }


你可能感兴趣的:(ios,swift,数据存储)