How to encode a value passed through a delegate using NSCoding

最后都变了- 提交于 2019-12-25 08:08:34

问题


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:


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


来源:https://stackoverflow.com/questions/41360491/how-to-encode-a-value-passed-through-a-delegate-using-nscoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!