import UIKit
@objcMembers//// 在swift 4.0 之后編輯器是不會(huì)對(duì)屬性加 隱式的 @objc 的 如果不加 @objcMembers 運(yùn)行時(shí)是獲取不到屬性和ivar 的
class BaseCodingModel: NSObject, NSCoding {
override init() {
super.init()
}
// MARK: - 歸檔
func encode(with aCoder: NSCoder) {
for name in CodeUserModel.propertyList() {
guard let value = self.value(forKey: name) else {
return
}
aCoder.encode(value, forKey: name)
}
}
// MARK: - 解檔
required init?(coder aDecoder: NSCoder) {
}
// MARK: - 獲取屬性數(shù)組
class func propertyList() -> [String] {
var count: UInt32 = 0 // 下面會(huì)改變count 所以要用 var
// 需要的是一個(gè) UInt32 類型的指針
// 在 class 方法中 self 指的是本類 不是本類對(duì)象
let list = class_copyPropertyList(self, &count)
var arrayName:[String] = []
//此時(shí) count 是 UInt32類型的 list是 '屬性' 的 數(shù)組
for i in 0..<Int(count) {
let property = list?[i]
// 獲取屬性的名稱 獲取到的是 Int8 類型的 是c 的字符串
let cName = property_getName(property!)
// 將c 的字符串轉(zhuǎn)化為 Swift 的字符串
let name = String(utf8String: cName)
arrayName.append(name!)
}
return arrayName
}
// MARK: - ivar列表
class func ivarList() -> [String] {
// 于獲取屬性類似
var count: UInt32 = 0
let list = class_copyIvarList(self, &count)
var arrayName:[String] = []
for i in 0..<Int(count) {
guard let ivar = list?[i],
let cName = ivar_getName(ivar),
let name = String(utf8String: cName)
else {
continue //結(jié)束此次循環(huán)
}
arrayName.append(name)
}
return arrayName
}
}
import UIKit
class CodeUserModel: BaseCodingModel {
//單列
static let sharedUserModel = CodeUserModel()
var isLogin: String = "false"
var customer_id: String = ""
var mobile: String = ""
var token: String = ""
private let userModelKey = "myUserModel"
override init() {
super.init()
}
// MARK: - 解檔
required init?(coder aDecoder: NSCoder) {
super.init()
for name in CodeUserModel.propertyList() {
guard let value = aDecoder.decodeObject(forKey: name) else {
return
}
setValue(value, forKeyPath: name)
}
}
// MARK: 查詢
func unarchive () {
do {
let dataFrom = kUserDefaults.data(forKey: userModelKey)
if dataFrom != nil {
if let codeUser = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(dataFrom!) as? CodeUserModel {
customer_id = codeUser.customer_id
mobile = codeUser.mobile
token = codeUser.token
isLogin = codeUser.isLogin
printLog(message: "查詢用戶信息---\(customer_id)---\(mobile)---\(token)---\(isLogin)")
}
}
} catch {
printLog(message: "獲取失敗了")
}
}
// MARK: 保存
func archive(user:CodeUserModel) {
let data = NSKeyedArchiver.archivedData(withRootObject: user)
kUserDefaults.set(data, forKey: userModelKey)
kUserDefaults.synchronize()
}
// MARK: 退出登錄
func logout() {
//清空數(shù)據(jù)
let data = NSKeyedArchiver.archivedData(withRootObject: CodeUserModel())
kUserDefaults.set(data, forKey: userModelKey)
kUserDefaults.synchronize()
}
}
//讀取信息
let useModel = CodeUserModel.sharedUserModel
useModel.unarchive()
//登錄保存信息
var userModel:CodeUserModel = OCUserModel.sharedUserModel
userModel = CodeUserModel.mj_object(withKeyValues: responseObject)
userModel.isLogin = "true"
codeUser.archive(user: codeUser)
//退出登錄
CodeUserModel.sharedUserModel.logout()