资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

NSUserDefault保存自定义对象-创新互联

//

创新互联是一家专业的成都网站建设公司,我们专注网站设计、成都网站设计、网络营销、企业网站建设,卖友情链接一元广告为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。

// User.swift

//  情感夜空

//

// Created by 柯木超 on 15/10/7.

// Copyright  2015年 柯木超. All rights reserved.

//

import UIKit

import Foundation

import SwiftyJSON

class User: NSObject , NSCoding{

  var icon:String?; //头像

  var name:String?; //昵称

  var loginName:String?; //登录名

  var password:String?; //登录名

  var brityday:String?; //出生日期

  var occupation:String?; //职业

  var interest:String?; //兴趣爱好

  var emotionStatus:String?; //情感状态

  var city:City?; //城市

  var coordinateX:String?; //横坐标(经度)

  var coordinateY:String?; //纵坐标(纬度)

  var sex:String?; //性别

  var gender:String?; //星座

  var token:String?; //融云token

  var hight:String?; //身高

  init(json:JSON) {

    super.init();

    self.name = json["name"].stringValue;

    self.loginName = json["loginName"].stringValue;

    self.password = json["password"].stringValue;

    self.brityday = json["brityday"].stringValue;

    self.occupation = json["occupation"].stringValue;

    self.interest = json["interest"].stringValue;

    self.emotionStatus = json["emotionStatus"].stringValue;

    self.city = City(json: json["city"])

    self.coordinateX = json["coordinateX"].stringValue;

    self.coordinateY = json["coordinateY"].stringValue;

    self.sex = json["sex"].stringValue;

    self.gender = json["gender"].stringValue;

    self.token = json["token"].stringValue;

    self.hight = json["hight"].stringValue;

  }

  override init(){

  }

  func encodeWithCoder(aCoder: NSCoder) {

    aCoder.encodeObject(self.name, forKey: "name");

    aCoder.encodeObject(self.loginName, forKey: "loginName");

    aCoder.encodeObject(self.password, forKey: "password");

    aCoder.encodeObject(self.brityday, forKey: "brityday");

    aCoder.encodeObject(self.occupation, forKey: "occupation");

    aCoder.encodeObject(self.interest, forKey: "interest");

    aCoder.encodeObject(self.emotionStatus, forKey: "emotionStatus");

    aCoder.encodeObject(self.city, forKey: "city");

    aCoder.encodeObject(self.coordinateX, forKey: "coordinateX");

    aCoder.encodeObject(self.coordinateY, forKey: "coordinateY");

    aCoder.encodeObject(self.sex, forKey: "sex");

    aCoder.encodeObject(self.gender, forKey: "gender");

    aCoder.encodeObject(self.token, forKey: "token");

    aCoder.encodeObject(self.hight, forKey: "hight");

  }

  required init?(coder aDecoder: NSCoder) {

    self.name = aDecoder.decodeObjectForKey("name") as? String;

    self.loginName = aDecoder.decodeObjectForKey("loginName") as? String;

    self.password = aDecoder.decodeObjectForKey("password") as? String;

    self.brityday = aDecoder.decodeObjectForKey("brityday") as? String;

    self.occupation = aDecoder.decodeObjectForKey("occupation") as? String;

    self.interest = aDecoder.decodeObjectForKey("interest") as? String;

    self.emotionStatus = aDecoder.decodeObjectForKey("emotionStatus") as? String;

    self.city = aDecoder.decodeObjectForKey("city") as? City;

    self.coordinateX = aDecoder.decodeObjectForKey("coordinateX") as? String;

    self.coordinateY = aDecoder.decodeObjectForKey("coordinateY") as? String;

    self.sex = aDecoder.decodeObjectForKey("sex") as? String;

    self.gender = aDecoder.decodeObjectForKey("gender") as? String;

    self.token = aDecoder.decodeObjectForKey("token") as? String;

    self.hight = aDecoder.decodeObjectForKey("hight") as? String;

  }

}

 //用户用user default保存

 let userData = NSKeyedArchiver.archivedDataWithRootObject(user);

 let userDefault = NSUserDefaults.standardUserDefaults();

 userDefault.setValue(userData, forKey: "user”);

//取

let userdata:NSData = (NSUserDefaults.standardUserDefaults().valueForKey("user") as? NSData)!;

//反序列化

let user:User = (NSKeyedUnarchiver.unarchiveObjectWithData(userdata) as? User)!;

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:NSUserDefault保存自定义对象-创新互联
分享URL:http://cdkjz.cn/article/djopjp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220