1.在class OAuthViewController:UIViewController
// 3.從字典中取出昵稱和用戶頭像地址(account :UserAccount)
account.screen_name = userInfoDict["screen_name"]as?String
account.avatar_large = userInfoDict["avatar_large"]as?String
// 4.將account對(duì)象保存
NSKeyedArchiver.archiveRootObject(account, toFile: UserAccountViewModel.shareIntance.accountPath)
2.在class UserAccount:NSObject,NSCoding?
// MARK:-歸檔&解檔
///解檔的方法
requiredinit?(coder aDecoder:NSCoder) {
? ? ? ? ?access_token= aDecoder.decodeObjectForKey("access_token")as?String
? ? ? ? ?uid= aDecoder.decodeObjectForKey("uid")as?String
? ? ? ? ?expires_date= aDecoder.decodeObjectForKey("expires_date")as?NSDate
? ? ? ? ?avatar_large= aDecoder.decodeObjectForKey("avatar_large")as?String
? ? ? ? ?screen_name= aDecoder.decodeObjectForKey("screen_name")as?String
}
///歸檔方法
funcencodeWithCoder(aCoder:NSCoder) {
? ? ? ? aCoder.encodeObject(access_token, forKey:"access_token")
? ? ? ?aCoder.encodeObject(uid, forKey:"uid")
? ? ? ?aCoder.encodeObject(expires_date, forKey:"expires_date")
? ? ? ?aCoder.encodeObject(avatar_large, forKey:"avatar_large")
? ? ? ?aCoder.encodeObject(screen_name, forKey:"screen_name")
}
3.在其它類中提取沙河里的值
// 1.從沙盒中讀取中歸檔的信息
var accountPath =NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask,true).first!
accountPath = (accountPath as NSString).stringByAppendingPathComponent("account.plist")
account=NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount
//取出值,如下
account.screen_name