手机号码
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!
// 创建一个全局路径,即要保存到闪存的位置
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")
// 保存数组
//
}
@IBAction func archveAction(_ sender: AnyObject) {
// 归档
contactArr = ["a", "b", 1]
NSKeyedArchiver.archiveRootObject(self.contactArr!, toFile: ContactFilePath)
}
@IBAction func unArchveAction(_ sender: AnyObject) {
// 从归档中读取给数组,如果第一次读取无数据,则实例化数组
// 这里要用到 解档方法
NSKeyedUnarchiver.unarchiveObject(withFile: ContactFilePath)
var contactArr:NSMutableArray?
if(contactArr == nil){
print("从归档中提取")
contactArr = NSKeyedUnarchiver.unarchiveObject(withFile: ContactFilePath) as! NSMutableArray!
print(contactArr)
if(contactArr == nil){
print("归档中没有,创建数组")
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("归档中没有,创建数组")
self.userDic = UserModel.init()
}
}
}
}