溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

NSUserDefault 保存自定義對(duì)象

發(fā)布時(shí)間:2020-06-14 17:27:03 來源:網(wǎng)絡(luò) 閱讀:536 作者:柯木超 欄目:移動(dòng)開發(fā)

//

//  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?; //職業(yè)

    var  interest:String?; //興趣愛好

    var  emotionStatus:String?; //情感狀態(tài)

    var  city:City?; //城市

    var  coordinateX:String?; //橫坐標(biāo)(經(jīng)度)

    var  coordinateY:String?; //縱坐標(biāo)(緯度)

    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)!;



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI