Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

ios - How to encode a value passed through a delegate using NSCoding

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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...