酱油01-NSUserDefaults/NSKeyedArchiver

NSUserDefaults与NSKeyedArchiver

*NSUserDefaults适用于小部分的数据存储

  • 三种不同的存储数据的方式
import UIKit

class ViewController: UIViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        //只能写入Bool/NSString/NSNumber/NSURL/NSArray/NSDictionary的数据
        
        //每一种都有设置数据的方法与其对应的取数据的方法
        let q = NSUserDefaults.standardUserDefaults()
        q.setBool(true, forKey: "key1")
        let q1 = q.boolForKey("key1")

        print(q1)
        
        q.setObject("等君来", forKey: "Name")
        let q2 = q.objectForKey("Name")
        print(q2)
        
        let url = NSURL(string: "http://tq.91.com/api/?&city=1010101001101000002&act=210&nt=%2210&pid=168&mt=01")
        q.setURL(url, forKey: "URL")
        let q3 = q.URLForKey("URL")
        print(q3)
        
        q.setDouble(2.47, forKey: "Double1")
        let q4 = q.doubleForKey("Double1")
        print(q4)
        
        q.setValuesForKeysWithDictionary(["name" : "zhanshanjun"])
        let q5 = q.dictionaryWithValuesForKeys(["name"])
        print(q5)
        
        let array = [1,2,3]
        q.setObject(array, forKey: "array")
        let q6 = q.objectForKey("array")
        print(q6)

        //而NSString/NSArray/NSDictionary所对应的数据有自己的write方法,将数据写到本地的地址中
        
        let s1 : NSString = "free"
        let path = NSHomeDirectory() + "/Documents/1.plist"
        try! s1.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)
        
        print(path)
        
        let a1 : NSArray = [1,2,3,4,5,"tyu"]
        let path1 = NSHomeDirectory() + "/Documents/2.plist"
        a1.writeToFile(path1, atomically: true)
        print(a1)
        
        let d1 : NSDictionary = ["key1" : 44]
        let path2 = NSHomeDirectory() + "/Documents/3.plist"
        d1.writeToFile(path2, atomically: true)
        print(d1)
        
        
        //创建一个king的类,用类成员存储数据,使用NSKeyedArchiver的成员方法将数据存储在本地地址中,用NSKeyedUnarchiver将数据
        let k = king()
        k.age = 12.02
        k.name = "Rex"
        NSKeyedArchiver.archiveRootObject(k, toFile: NSHomeDirectory() + "/Documents/1.plist")
        print(NSHomeDirectory() + "/Documents/1.plist")
        
        let k1 = NSKeyedUnarchiver.unarchiveObjectWithFile(NSHomeDirectory() + "/Documents/1.plist") as? king
        print(k1?.age, k1?.name)
        
        let k3 = king()
        k3.age = 14.21
        k3.name = "Ted"
        
        //测试存储数组
        let a = [k,k3]
        NSKeyedArchiver.archiveRootObject(a, toFile: NSHomeDirectory() + "/Documents/2.plist")
        let k2 = NSKeyedUnarchiver.unarchiveObjectWithFile(NSHomeDirectory() + "/Documents/2.plist") as? NSArray
        let k4 = k2![0] as? king
        print(k4?.age,k4?.name)
        
        //将数据转存到内存中
        let data = NSKeyedArchiver.archivedDataWithRootObject(a)
        NSUserDefaults.standardUserDefaults().setObject(data, forKey: "data")
        
        let data1 = NSUserDefaults.standardUserDefaults().objectForKey("data") as? NSData
        let data2 = NSKeyedUnarchiver.unarchiveObjectWithData(data1!)
        print(data2)
    }
    
}

*king类

import UIKit

//NSCoder 用于编码,可转换多种数据格式
class king: NSCoder {
    var age : Double!
    var name : String!
    override init() {
        super.init()
        
    }
    required init(coder aDecoder: NSCoder) {
        name = aDecoder.decodeObjectForKey("name") as? String
        age = aDecoder.decodeDoubleForKey("age")
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeDouble(age, forKey: "age")
    }

}

你可能感兴趣的:(酱油01-NSUserDefaults/NSKeyedArchiver)