Model.swift
import UIKit
class UserModel: NSObject,NSCoding {
var username:String = ""
var password: NSInteger = 0
var type: Int = -1
required init(coder aDecoder: NSCoder) {
super.init()
username = (aDecoder.decodeObject(forKey: "username") as! NSString) as String
password = aDecoder.decodeInteger(forKey: "password")
type = aDecoder.decodeInteger(forKey: "type")
}
override init() {
}
func encode(with aCoder: NSCoder) {
aCoder.encode(username, forKey: "username")
aCoder.encode(password, forKey: "password")
aCoder.encode(type, forKey: "type")
}
/* methods */
class func initUserModelWithDic(dic:[String:Any]) -> UserModel {
let userModel: UserModel = UserModel.init()
userModel.username = dic["username"] as! String
userModel.password = dic["password"] as! NSInteger
return userModel
}
func insertData(dic:[String:Any]) {
self.type = dic["type"] as! Int
}
}
歸檔.h
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
// 創(chuàng)建一個(gè)全局路徑,即要保存到閃存的位置
let ContactFilePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0].appending("/contacts.data")
var contactArr:NSMutableArray?
override func viewDidLoad() {
super.viewDidLoad()
initUI()
}
func initUI() {
//let ContactFilePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.DocumentDirectory, FileManager.SearchPathDomainMask.UserDomainMask, true [0].stringByAppendingPathComponent("contacts.data")
// 保存數(shù)組
//
}
@IBAction func archveAction(_ sender: AnyObject) {
// 歸檔
contactArr = ["a", "b", 1]
NSKeyedArchiver.archiveRootObject(self.contactArr!, toFile: ContactFilePath)
}
@IBAction func unArchveAction(_ sender: AnyObject) {
// 從歸檔中讀取給數(shù)組,如果第一次讀取無數(shù)據(jù),則實(shí)例化數(shù)組
// 這里要用到 解檔方法
NSKeyedUnarchiver.unarchiveObject(withFile: ContactFilePath)
var contactArr:NSMutableArray?
if(contactArr == nil){
print("從歸檔中提取")
contactArr = NSKeyedUnarchiver.unarchiveObject(withFile: ContactFilePath) as! NSMutableArray!
print(contactArr)
if(contactArr == nil){
print("歸檔中沒有,創(chuàng)建數(shù)組")
self.contactArr = NSMutableArray()
}
}
}
}
模型解歸檔.h
import UIKit
class ViewController2: UIViewController {
let ContactFilePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0].appending("/userModel.data")
var userDic: UserModel?
override func viewDidLoad() {
super.viewDidLoad()
initUI()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func initUI() {
}
@IBAction func archveModel(_ sender: AnyObject) {
let dic = [
"username":"liao",
"password":123456
] as [String : Any]
let user = UserModel.initUserModelWithDic(dic: dic)
let dic2 = [
"type" : 1
]
user.insertData(dic: dic2)
print("archive之前\(user)")
let b_suc:Bool = NSKeyedArchiver.archiveRootObject(user, toFile: ContactFilePath)
if b_suc {
print("歸檔模型成功")
}else {
print("歸檔模型失敗")
}
}
@IBAction func unarchveModel(_ sender: AnyObject) {
// 這里要用到 解檔方法
NSKeyedUnarchiver.unarchiveObject(withFile: ContactFilePath)
var contactArr:UserModel?
if(contactArr == nil){
print("從歸檔中提取")
contactArr = NSKeyedUnarchiver.unarchiveObject(withFile: ContactFilePath) as? UserModel
print((contactArr?.username)! + " " + String(describing: contactArr!.type))
if(contactArr == nil){
print("歸檔中沒有,創(chuàng)建數(shù)組")
self.userDic = UserModel.init()
}
}
}
}