2016-09-26 4 views
-1

ObjectMapper를 사용하여 내 서버에서 JSON 응답을 매핑하고 있습니다. 여기 내 데이터 모델입니다.전체 개체를 NSUserDefaults에 저장하는 방법

class HomeStats: Mappable { 

// MARK: - Constants & Variables 

var todayText: String 
var pointsText: String 
var todayActivitiesText: String 
var totalPointsText: String 
var rewardsText: String 
var myStatsText: String 
var motivationalMessage: String 

var todaySteps: String 
var todayStepPoints: String 
var stepsText : String 
var todayTotalPoints: Double 
var allPoints: String 

var userRegistrationDate: String 
var firstTrackerConnectionDate: String 

var userID:Int 
. 
. 
. 

등등. 나는 지금은 NSUserDefaults이 경우에 내 모든 객체 allStats를 저장하고 나중에 검색 할 수있는 방법을

// init 
let allStats = Mapper<HomeStats>().map([:])! 

// usage 
if let data = Mapper<HomeStats>().map(result){ // result is my JSON responce 
     self.allStats = data; 
} 

로 내 수업이 사용하고? 당신이 단일 개체로 개체를 저장하려면

+0

http://stackoverflow.com/questions/2315948/how-to-store-custom-objects -in-nsuserdefaults 이것 좀 봐 –

+0

나는 그 해답을 보았지만, 나는 별도의 키로 everyThing을 저장해야하기 때문에 나에게 최적화 된 접근 방식으로 보이지 않는다. 전체 개체를 키에 대해 저장하고 다시 가져 오려고합니다. – Byte

+0

NSUserDefaults는 저장할 수 있습니다 - NSArray를 을 -을 NSData - NSDictionary에 -의 NSNumber -는 NSString 또한,있는 NSArray 나 NSDictionary와는 위에 열거 된 유형을 포함해야합니다. 따라서 구조를 NSDictionary로 변환하거나 대신 JSON 응답을 저장해야합니다. – Roman

답변

0

당신의 방법은 NSCoding을 준수하는 것 :

class HomeStats: NSObject, Mappable, NSCoding { 

    // add to your class HomeStats 

    required init?(coder aDecoder: NSCoder) { 
     todayText = aDecoder.decodeObjectForKey(todayText) as! String 
     pointsText = aDecoder.decodeObjectForKey(pointsText) as! String 
     todayActivitiesText = aDecoder.decodeObjectForKey(todayActivitiesText) as! String 
     totalPointsText = aDecoder.decodeObjectForKey(totalPointsText) as! String 
     rewardsText = aDecoder.decodeObjectForKey(rewardsText) as! String 
     myStatsText = aDecoder.decodeObjectForKey(myStatsText) as! String 
     motivationalMessage = aDecoder.decodeObjectForKey(motivationalMessage) as! String 

     todaySteps = aDecoder.decodeObjectForKey(todaySteps) as! String 
     todayStepPoints = aDecoder.decodeObjectForKey(todayStepPoints) as! String 
     stepsText = aDecoder.decodeObjectForKey(stepsText) as! String 
     todayTotalPoints = aDecoder.decodeDoubleForKey(todayTotalPoints) as! Double 
     allPoints = aDecoder.decodeObjectForKey(allPoints) as! String 

     userRegistrationDate = aDecoder.decodeObjectForKey(userRegistrationDate) as! String 
     firstTrackerConnectionDate = aDecoder.decodeObjectForKey(firstTrackerConnectionDate) as! String 

     userID = aDecoder.decodeIntForKey(userID, forKey: "userID") as! Int 
    } 

    func encodeWithCoder(aCoder: NSCoder) { 
     aCoder.encodeObject(todayText, forKey: "todayText") 
     aCoder.encodeObject(pointsText, forKey: "pointsText") 
     aCoder.encodeObject(todayActivitiesText, forKey: "todayActivitiesText") 
     aCoder.encodeObject(totalPointsText, forKey: "totalPointsText") 
     aCoder.encodeObject(rewardsText, forKey: "rewardsText") 
     aCoder.encodeObject(myStatsText, forKey: "myStatsText") 
     aCoder.encodeObject(motivationalMessage, forKey: "motivationalMessage") 

     aCoder.encodeObject(todaySteps, forKey: "todaySteps") 
     aCoder.encodeObject(todayStepPoints, forKey: "todayStepPoints") 
     aCoder.encodeObject(stepsText, forKey: "stepsText") 
     aCoder.encodeDouble(todayTotalPoints, forKey: "todayTotalPoints") 
     aCoder.encodeObject(allPoints, forKey: "allPoints") 

     aCoder.encodeObject(userRegistrationDate, forKey: "userRegistrationDate") 
     aCoder.encodeObject(firstTrackerConnectionDate, forKey: "firstTrackerConnectionDate") 

     aCoder.encodeInteger(userID, forKey: "userID") 
    } 
} 

// save and load your serialised file wherever you wish with something like this: 

let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) 
let documentsDirectory = paths[0] 
let filePath = documentsDirectory.appendingPathComponent("filename") 

// serialise and save your instance 
func storeHomeStats(homeStats: HomeStats) { 
    let data = NSKeyedArchiver.archivedData(withRootObject: homeStats) 

    do { 
     try data.write(to: filePath) 
    } catch let error as NSError { 
     print("Couldn't write file: \(error)") 
    } 
} 

// deserialise your instance 
func loadHomeStats() -> HomeStats? { 
    return NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? HomeStats 
}