iOS數(shù)據(jù)持久化方式可分為三種:如下圖
歸檔存儲(chǔ) NSKeyedArchiver
對(duì)象要實(shí)現(xiàn)NSCoding協(xié)議瑟由,歸檔 encode芜赌,解檔 decode
歸檔和解檔都需要加載整個(gè)文件
①對(duì)已有類型歸檔和解檔
方式一 :一次只能歸檔一個(gè)對(duì)象
//歸檔數(shù)據(jù)
func archiveData() {
//獲取文件路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = path.stringByAppendingString("/data.archive")
//歸檔
let array = ["德瑪西亞", "猴哥"]
if NSKeyedArchiver.archiveRootObject(array, toFile: filePath) {
print("Archive Success")
}
}
//解檔數(shù)據(jù)
func unarchiveData() {
//獲取文件路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = path.stringByAppendingString("/data.archive")
//解檔
let data = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as! NSArray
print(data)
}
方式二 :一次存儲(chǔ)多個(gè)對(duì)象
/*
??NSData 和它子類 NSMutableData 是字節(jié)緩沖區(qū)的對(duì)象化封裝
這個(gè)兩個(gè)類通常在數(shù)據(jù)存儲(chǔ)時(shí)候用的
NSMutableData 是動(dòng)態(tài)數(shù)據(jù)對(duì)象作儿。
即我們可以先創(chuàng)建個(gè)空的對(duì)象實(shí)例豆茫,在對(duì)象有效范圍內(nèi)增加迅箩、刪除數(shù)據(jù)內(nèi)容
*/
//歸檔數(shù)據(jù)
func archiveData() {
//獲取文件路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = path.stringByAppendingString("/data.archive")
///歸檔
let data = NSMutableData() //*??
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
//歸檔對(duì)象
archiver.encodeObject("LOL", forKey: "game")
archiver.encodeObject(["小炮", "卡牌"], forKey: "name")
archiver.encodeInt(5, forKey: "age")
archiver.finishEncoding() //完成歸檔
data.writeToFile(filePath, atomically: true) //寫(xiě)入文件
}
//解檔數(shù)據(jù)
func unarchiveData() {
//獲取文件路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = path.stringByAppendingString("/data.archive")
///解檔
let unarchiveData = NSData(contentsOfFile: filePath)
let unarchiver = NSKeyedUnarchiver(forReadingWithData: unarchiveData!)
//解檔對(duì)象
let decodeStr = unarchiver.decodeObjectForKey("game") as! String
let decodeArr = unarchiver.decodeObjectForKey("name") as! NSArray
let decodeInt = unarchiver.decodeIntForKey("age")
print("\(decodeStr) \(decodeArr) \(decodeInt)")
}
②自定義類型歸檔和解檔
必須實(shí)現(xiàn) NSCoding 協(xié)議
class CustomDataModel: NSObject, NSCoding {
var numberArr: NSArray!
var name: String!
//歸檔
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.numberArr, forKey: "numberArr")
aCoder.encodeObject(self.name, forKey: "name")
}
required init?(coder aDecoder: NSCoder) {
super.init()
//解檔
self.numberArr = aDecoder.decodeObjectForKey("numberArr") as! NSArray
self.name = aDecoder.decodeObjectForKey("name") as! String
}
override init() {
}
}
-使用
//歸檔數(shù)據(jù)
func archiveData() {
//獲取文件路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = path.stringByAppendingString("/data.archive")
///歸檔
let yourData = CustomDataModel()
yourData.numberArr = [1,2,3]
yourData.name = "蓋倫"
if NSKeyedArchiver.archiveRootObject(yourData, toFile: filePath) {
print("Archive Success")
}
}
//解檔數(shù)據(jù)
func unarchiveData() {
//獲取文件路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = path.stringByAppendingString("/data.archive")
///解檔
let unarchiveData = NSData(contentsOfFile: filePath) //*??
let decodeData = NSKeyedUnarchiver.unarchiveObjectWithData(unarchiveData!)
print(decodeData?.numberArr)
print(decodeData?.name)
}
屬性列表
NSUserDefaults溉愁、 plist
① NSUserDefaults
注意:必須保證每一個(gè)Key的唯一性,相同的key會(huì)被覆蓋
/*
支持的數(shù)據(jù)類型有NSString饲趋、 NSNumber拐揭、NSDate、 NSArray奕塑、
NSDictionary堂污、BOOL、NSInteger爵川、NSFloat等系統(tǒng)定義的數(shù)據(jù)類型
如果要存放自定義的對(duì)象敷鸦,可將其轉(zhuǎn)換成 NSData 進(jìn)行存儲(chǔ)
*/
//存儲(chǔ)
func saveNSUserDefaults() {
let defults = NSUserDefaults.standardUserDefaults()
defults.setObject("汗血寶馬", forKey: "name") //存儲(chǔ)數(shù)據(jù)
defults.synchronize() //同步數(shù)據(jù)
}
//讀取
func readNSUserDefaults() {
let defults = NSUserDefaults.standardUserDefaults()
let name = defults.objectForKey("name") as! String//取出數(shù)據(jù)
print(name)
}
② plist
//存儲(chǔ)
func savePlistFile() {
//獲取Document路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
//獲取文件路徑
let filePath = path.stringByAppendingString("/data.plist")
let data = NSMutableArray() //*??
//添加對(duì)象
data.addObject("蘭博基尼")
data.addObject("法拉利")
data.addObject("布加迪威龍")
data.addObject("保時(shí)捷")
data.addObject("寶馬")
//寫(xiě)入文件
data.writeToFile(filePath, atomically: true)
}
//讀取
func readPlistFile() {
//獲取Document路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
//獲取文件路徑
let filePath = path.stringByAppendingString("/data.plist")
let data = NSArray(contentsOfFile: filePath)
print(data)
}
數(shù)據(jù)庫(kù)
CoreData、SQLite寝贡、第三方庫(kù)
多數(shù)據(jù)最好還是用數(shù)據(jù)庫(kù)存儲(chǔ)
CoreData 出門(mén)右轉(zhuǎn) 官方文檔 或度娘
SQLite 出門(mén)搜索引擎不送
第三方庫(kù) github 走起....
后續(xù)...