数据存储

归档方法存储

class Animal: NSObject, NSCoding {
    var name: NSString!
    var weight: Double!
    
    override init() {
        super.init()
    }
    
    //取
    required init?(coder aDecoder: NSCoder) {
        name = aDecoder.decodeObjectForKey("name") as! NSString
        weight = aDecoder.decodeDoubleForKey("weight")
    }
    
    //存
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeDouble(weight, forKey: "weight")
    }
}


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let ani = Animal()
        ani.name = "Cat"
        ani.weight = 23
        
        //NSString/NSArray...
        //要求所有对象都实现了NSCoding协议
        //自动实现对象与存储数据之间的转换
        //缺点:苹果自有的格式,不支持其它平台
        
        //归档
        NSKeyedArchiver.archiveRootObject(ani, toFile: NSHomeDirectory() + "/Documents/1.plist")
        
        print(NSHomeDirectory())
        
        //解归档
        let ani1 = NSKeyedUnarchiver.unarchiveObjectWithFile(NSHomeDirectory() + "/Documents/1.plist") as! Animal
        print(ani1.name, ani1.weight)
        
        
        let ani2 = Animal()
        ani2.name = "Dog"
        ani2.weight = 45
        
        let arr = [ani, ani2]
        NSKeyedArchiver.archiveRootObject(arr, toFile: NSHomeDirectory() + "/Documents/2.plist")
        
        let arr1 = NSKeyedUnarchiver.unarchiveObjectWithFile(NSHomeDirectory() + "/Documents/2.plist") as! NSArray
        
        print(arr1)
        let ani3 = arr1[0] as! Animal
        print(ani3.name, ani3.weight)
        
        //获取一个NSData,把归档以后的数据存入内存
        let data = NSKeyedArchiver.archivedDataWithRootObject(arr)
        NSUserDefaults.standardUserDefaults().setObject(data, forKey: "data")
        
        let data1 = NSUserDefaults.standardUserDefaults().objectForKey("data") as! NSData
        let arr2 = NSKeyedUnarchiver.unarchiveObjectWithData(data1)
        print(arr2)
    }

```
#write方法存储
```swift
//NSString/NSArray/NSDictioanry/NSData..
        //write....
        let s: NSString = "abc"
        
        let path = NSHomeDirectory() + "/Documents/1.plist"
        try! s.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)
        
        let s1 = try! NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding)
        print(s1)
        
        let arr: NSArray = ["abc", "123", 123]
        arr.writeToFile(NSHomeDirectory() + "/Documents/2.plist", atomically: true)
        
        let arr1 = NSArray(contentsOfFile: NSHomeDirectory() + "/Documents/2.plist")
        print(arr1)
        
        let ani = AnimalModel()
        ani.name = "Dog"
        ani.weight = 20
        
        let dict = ani.toDictionary()
        
        let arr2: NSArray = [dict]
        let b = arr2.writeToFile(NSHomeDirectory() + "/Documents/3.plist", atomically: true)
        if b {
            print("存储成功")
        }
        else {
            print("存储失败")
        }
        
        let arr3 = NSArray(contentsOfFile: NSHomeDirectory() + "/Documents/3.plist")
        print(arr3)
        
        print(NSHomeDirectory() + "/Documents")
        
        let dict1 = arr3![0]
        let ani1 = AnimalModel(dict: dict1 as! NSDictionary)
        
        print(ani1.name, ani1.weight)
    }

```
#UserDefaults方法存储
```swift
        let ud = NSUserDefaults.standardUserDefaults()
        ud.setBool(true, forKey: "key1")
        ud.setObject("doubi", forKey: "key3")
//        ud.setInteger(abc, forKey: "key")
        
        ud.setURL(NSURL(string: "www.baidu.com"), forKey: "url")
        let k1 = ud.boolForKey("key1")
        let k2 = ud.floatForKey("key2")
        let k3 = ud.objectForKey("key3")
        let k4 = ud.URLForKey("url")!
        print(k1, k2, k3, k4)
```

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