2016-09-26 35 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 
. 
. 
. 

依此類推。我在我的課作爲

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

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

現在我怎麼可以存儲我的整個對象allStats在這種情況下NSUserDefaults,並在以後檢索使用呢?

+0

http://stackoverflow.com/questions/2315948/how-to-store-custom-objects -in-nsuserdefaults 看看這個 –

+0

我已經看到了這些答案,但它們似乎並不是我最優化的方法,因爲我必須用一個單獨的鍵保存每個Thing。我想將整個對象保存爲一個鍵並將其還原。 – 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 
} 
相關問題