/**
注:用于文件保存的全局地址(方便修改)
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
struct Key {
struct Path {
static let Documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
static let arrayPath = "\(Documents)/array.plist"
static let DictionaryPath = "\(Documents)/dictionary.plist"
struct PathKey{
static let Name = "Name"
static let Age = "Age"
}
}
}
...
}
**/
1 创建NSArray数组
let array:NSArray = ["a","b","c","d","e"]
2 NSArray写入文件
//参数1 保存地址 AppDelegate.Key.Path.arrayPath
//参数2 true(先保存到tem中,等全部写完系统确定无误后在保存到文件中)
array.write(toFile: AppDelegate.Key.Path.arrayPath, atomically: true)
3 读取NSArray内容
//可在全项目中读取
let readArray = NSArray(contentsOfFile: AppDelegate.Key.Path.arrayPath)
print(readArray![2])
4 NSDictionary 同理
//保存
let dictionary:NSDictionary = [AppDelegate.Key.Path.PathKey.Name:"lisi",AppDelegate.Key.Path.PathKey.Age:"24"]
dictionary.write(toFile: AppDelegate.Key.Path.DictionaryPath, atomically: true)
//读取
let name:NSDictionary = NSDictionary(contentsOfFile: AppDelegate.Key.Path.DictionaryPath)!
print(name[AppDelegate.Key.Path.PathKey.Name]!)
print(name[AppDelegate.Key.Path.PathKey.Age]!)