How to encode a value passed through a delegate using NSCoding

793 views Asked by At

I'm really new using the NSCoding functionality, I'm trying to create persistence of data of an attributedString, particularly a UIColor that is passed through a delegate. I haven't found a tutorial that encodes values that are not declared and initialized in the same class in which the NSCoding protocol is conformed.

I have the following code, which is the method that conforms to the protocol I created, and assigns the passed color value as an attribute to the attributedString.

func didSelectColorCell(color: UIColor) {
    let coder = NSCoder.init()
    color.encode(with: coder)
    noteTextView.setAttributedValueAtSelectedTextRange(NSForegroundColorAttributeName, value: color)
}

The app crashes, and sends me a warning "cannot be sent to an abstract object of class NSCoder: Create a concrete instance!" I'm really lost on how to procede. I don't know how to adapt this tutorial http://nshipster.com/nscoding/ to my scenario.

Can someone please provide me guidance on how to order my ideas or how does NSCoding work with delegates? Any help would be appreciated :)

1

There are 1 answers

0
bubuxu On

Define a class to implement the NSCoding protocol (Playground sample):

class ColorHelper: NSObject, NSCoding {

    var color: UIColor?

    init(color: UIColor) {
        super.init()
        self.color = color
    }

    required init(coder aDecoder: NSCoder) {

        if let color = aDecoder.decodeObject(forKey: "color") as? UIColor {
            self.color = color
        }

    }

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

    func save(defaults key: String) -> Bool {

        let defaults = UserDefaults.standard
        let savedData = NSKeyedArchiver.archivedData(withRootObject: data)
        defaults.set(savedData, forKey: key)
        return defaults.synchronize()

    }

    convenience init?(defaults key: String) {

        let defaults = UserDefaults.standard
        if let data = defaults.object(forKey: key) as? Data,
            let obj = NSKeyedUnarchiver.unarchiveObject(with: data) as? ColorHelper,
            let color = obj.color {
            self.init(color: color)
        } else {
            return nil
        }

    }

}

let data = ColorHelper(color: .red)

let savedData = NSKeyedArchiver.archivedData(withRootObject: data)
let obj = NSKeyedUnarchiver.unarchiveObject(with: savedData) as? ColorHelper
obj?.color

In your code:

func didSelectColorCell(color: UIColor) {
    let helper = ColorHelper(color: color)
    helper.save(defaults: "color")
    noteTextView.setAttributedValueAtSelectedTextRange(NSForegroundColorAttributeName, value: color)
}

To read the saved data:

let color = ColorHelper(defaults: "color")?.color