-1

I am using ObjectMapper to map JSON responce from my server. Here is my data model.

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
.
.
.

And so on. I am using this in my class as

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

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

Now how can I store my whole object allStats in this case to NSUserDefaults and retrieve it later ?

Byte
  • 629
  • 2
  • 11
  • 29
  • http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults Take a look at this – Shayan Jalil Sep 26 '16 at 07:26
  • I have seen those answers, but they does not seems as optimized approach to me because I have to save everyThing with a seperate key. I want to save Whole object against a key and just get it back. – Byte Sep 26 '16 at 07:29
  • NSUserDefaults can store only – NSArray – NSData – NSDictionary – NSNumber – NSString Furthermore, the NSArray or NSDictionary must only contain the types listed above. So you need to convert your structure to NSDictionary or save a JSON response instead – Roman Sep 26 '16 at 07:44

1 Answers1

0

If you want to save your object as single object your way should be to conform to 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
}
RyuX51
  • 2,779
  • 3
  • 26
  • 33