如何使用NSCoding对通过委托传递的值进行编码 [英] How to encode a value passed through a delegate using NSCoding

查看:65
本文介绍了如何使用NSCoding对通过委托传递的值进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是使用NSCoding功能的新手,我正在尝试创建attributedString数据的持久性,尤其是通过委托传递的UIColor。我还没有找到一个教程来编码未声明和初始化的值的教程,该值在符合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.

我有以下代码,是符合我创建的协议的方法,并将传递的颜色值作为属性分配给attributedString。

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

应用程序崩溃,并向我发送警告无法发送到NSCoder类的抽象对象:创建一个具体实例!我真的迷失了如何继续前进。我不知道如何使本教程 http://nshipster.com/nscoding/ 适应我的情况

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.

有人可以向我提供有关如何整理我的想法或NSCoding如何与代表一起工作的指导吗?任何帮助将不胜感激:)

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

推荐答案

定义一个类以实现NSCoding协议(游乐场示例):

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

在您的代码中:

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

To读取保存的数据:

To read the saved data:

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

这篇关于如何使用NSCoding对通过委托传递的值进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆