标签:
/// 保存用户信息(保存在沙盒中)
- (void)saveUserInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// iOS 8.0 会直接写入沙盒,无需再使用 `[defaults synchronize];`
[defaults setObject:self.username forKey:SKUsernameKey];
[defaults setObject:self.pwd forKey:SKUserPasswordKey];
}
/// 加载用户下信息
- (void)loadUserInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.usernameText.text = [defaults stringForKey:SKUsernameKey];
self.pwdText.text = [defaults stringForKey:SKUserPasswordKey];
}
关于存档归档的方法
//1.归档路径,必须用静态变量 1.保存用户账户时参数必须要用静态变量2.静态变量只需要加载一次,等在调用的时候不需要在加载了
static private let accountPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!.stringByAppendingString("account.plist")
//2.保存用户账户,
func saveAccount() {
NSKeyedArchiver.archiveRootObject(self, toFile: UserAccount.accountPath)
}
// 解档 - 如果没有保存过,解档结果可能仍然是 nil
userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount
//MARK: 归档(这里面是我自己的定义的属性)
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(access_token, forKey: "access_token")
aCoder.encodeDouble(expires_in, forKey: "expires_in")
aCoder.encodeObject(expiresDate, forKey: "expiresDate")
aCoder.encodeObject(uid, forKey: "uid")
aCoder.encodeObject(name, forKey: "name")
aCoder.encodeObject(avatar_large, forKey: "avatar_large")
}
//解档
required init?(coder aDecoder: NSCoder) {
access_token = aDecoder.decodeObjectForKey("access_token") as? String
expires_in = aDecoder.decodeDoubleForKey("expires_in")
expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
uid = aDecoder.decodeObjectForKey("uid") as? String
name = aDecoder.decodeObjectForKey("name") as? String
avatar_large = aDecode
}
标签:
原文地址:http://www.cnblogs.com/jiangshengkai/p/4896553.html