1.plist 存储
1.利用沙盒根目录拼接“Documents”字符串
//存储
func saveArray()
{
// 1.获得沙盒根路径,不管是真机还是模拟机,用它是最合适不过了
let home:NSString =NSHomeDirectory()
print(home)
// 2.document路径
let docPath:NSString = home.stringByAppendingPathComponent("Documents")
// 3.新建数据
let data:NSArray = ["jack","ffff"]
//3.文件路径
let filepath:NSString = docPath.stringByAppendingPathComponent("data.plist")
//4.存储数据
data.writeToFile(filepathasString, atomically:true)
}
//读取
@IBActionfunc read() {
//1.获得沙盒根路径
let home:NSString =NSHomeDirectory()
//2.document路径
let doctPath:NSString = home.stringByAppendingPathComponent("Document")
//3.文件路径
let filepath:NSString = doctPath.stringByAppendingPathComponent("data.plist")
//4.读取数据
let data:NSArray? =NSArray(contentsOfFile: filepathasString)
print("%@",data)
}
// 不建议采用,因为新版本的可能会修改目录名
2.利用NSSearchPathForDirectoriesInDomains获取路径
let array: NSArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,false)
//2.document路径
let documents:NSString = array.objectAtIndex(0)as!NSString
print(documents)
2.偏好设置
//存储
@IBActionfunc save()
{
// 1.利用NSUserDefaults,就能直接访问软件的偏好设置(Library/Preferences)
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
print(NSHomeDirectory())//能看到手机根目录
// 2.存储数据
defaults.setObject("mj", forKey:"account")
defaults.setObject("123", forKey:"pwd")
defaults.setInteger(10, forKey:"age")
defaults.setBool(true, forKey:"auto_login")
// 3.立刻同步
defaults.synchronize()
}
//读取
@IBActionfunc read()
{
let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let account = defaults.objectForKey("account")
let autoLogin:Bool = defaults.boolForKey("auto_login")
print("%@ -- %d", account, autoLogin)
}
- 注意:UserDefaults设置数据时,不是立即写入,而是根据时间戳定时地把缓存中的数据写入本地磁盘。所以调用了set方法之后数据有可能还没有写入磁盘应用程序就终止了。出现以上问题,可以通过调用synchornize方法强制写入[defaults synchornize];
3.NSKeyedArchiver归档(NSCoding)
class MJStudent: NSObject,NSCoding
{
var no: NSString!
var height: Double!
var age: Int32!
/**
* 将某个对象写入文件时会调用
* 在这个方法中说清楚哪些属性需要存储
*/
func encodeWithCoder(aCoder: NSCoder)
{
print(NSHomeDirectory())
aCoder.encodeObject(self.no, forKey: "no")
aCoder.encodeInt(self.age, forKey: "age")
aCoder.encodeDouble(self.height, forKey: "height")
}
/**
* 从文件中解析对象时会调用
* 在这个方法中说清楚哪些属性需要存储
*/
required init?(coder aDecoder: NSCoder)
{
self.no = aDecoder.decodeObjectForKey("no") as! String
self.age = aDecoder.decodeIntForKey("age")
self.height = aDecoder.decodeDoubleForKey("height")
}
}
class ViewController: UIViewController {
//存储
@IBAction func save()
{
//1.新的模型对象
let stu: MJStudent = MJStudent(coder: aDecoder: NSCoder)
stu.no = "42343254"
stu.age = 20
stu.height = 1.55
// 2.归档模型对象
// 2.1.获得Documents的全路径
var doc: NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last
// 2.2.获得文件的全路径
var path: NSString = doc.stringByAppendingPathComponent("stu.data")
// 2.3.将对象归档
NSKeyedArchiver.archiveRootObject(stu, toFile: path as String)
}
//读取
@IBAction func read()
{
// 1.获得Documents的全路径
var doc: NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
// 2.获得文件的全路径
var path: NSString = doc.stringByAppendingPathComponent("stu.data")
// 3.从文件中读取MJStudent对象
let stu: MJStudent = NSKeyedUnarchiver.unarchiveObjectWithFile(path)
print("%@ %d %f", stu.no, stu.age, stu.height)
}
}