1

Part of my settings I want to save with UserDefaults.

I already found this solution here: Save custom objects into NSUserDefaults

But I could not figure out how to save if I have custom objects in a custom object.

My classes look like this:

class ConfigLabelMainList: NSObject, NSCoding {


    var labelMiddleFirst: StatsIntervalModel
    var labelMiddleSecond: StatsIntervalModel
    var labelMiddleThird: StatsIntervalModel
    var labelRightFirst: StatsIntervalModel
    var labelRightSecond: StatsIntervalModel

    init(labelMiddleFirst: StatsIntervalModel, labelMiddleSecond: StatsIntervalModel, labelMiddleThird: StatsIntervalModel, labelRightFirst: StatsIntervalModel, labelRightSecond: StatsIntervalModel) {
        self.labelMiddleFirst = labelMiddleFirst
        self.labelMiddleSecond = labelMiddleSecond
        self.labelMiddleThird = labelMiddleThird
        self.labelRightFirst = labelRightFirst
        self.labelRightSecond = labelRightSecond
    }

    func encode(with aCoder: NSCoder) {

    }

    required convenience init?(coder aDecoder: NSCoder) {

    }
}


class StatsIntervalModel: NSObject, NSCoding {
    var stat: String
    var interval: String

    init(stat: String, interval: String) {
        self.stat = stat
        self.interval = interval
    }

    required convenience init?(coder aDecoder: NSCoder) {
        let stat = aDecoder.decodeObject(forKey: "stat") as! String
        let interval = aDecoder.decodeObject(forKey: "interval") as! String
        self.init(stat: stat, interval: interval)
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(stat, forKey: "stat")
        aCoder.encode(interval, forKey: "interval")
    }

}

How would a solution look like?

1
  • There's no difference. Simply encode and decode the properties of ConfigLabelMainList just like you did in StatsIntervalModel. Commented Apr 25, 2018 at 15:29

1 Answer 1

2

Add given code in your class ConfigLabelMainList

required convenience init?(coder aDecoder: NSCoder) {

    let midFirst = aDecoder.decodeObject(forKey: "midFirst") as! StatsIntervalModel
    let midSecond = aDecoder.decodeObject(forKey: "midSecond") as! StatsIntervalModel
    let midThird = aDecoder.decodeObject(forKey: "midThird") as! StatsIntervalModel
    let rightFirst = aDecoder.decodeObject(forKey: "rightFirst") as! StatsIntervalModel
    let rightSecond = aDecoder.decodeObject(forKey: "rightSecond") as! StatsIntervalModel

    self.init(labelMiddleFirst: midFirst, labelMiddleSecond: midSecond, labelMiddleThird: midThird, labelRightFirst: rightFirst, labelRightSecond: rightSecond)
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(labelMiddleFirst, forKey: "midFirst")
    aCoder.encode(labelMiddleSecond, forKey: "midSecond")
    aCoder.encode(labelMiddleThird, forKey: "midThird")
    aCoder.encode(labelRightFirst, forKey: "rightFirst")
    aCoder.encode(labelRightSecond, forKey: "rightSecond")
}

Now simply archive data and save it in defaults.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.