简体   繁体   English

如何从已经存在的类创建自定义类? (现有类不支持NSCoding)

[英]How do I create a custom class from a class that already exists? (Existing class does not support NSCoding)

What I'm trying to do is convert an EKEvent into NSData , and then convert it back into an EKEvent . 我正在尝试将EKEvent转换为NSData ,然后将其转换回EKEvent

I looked around and noticed that in order to use NSKeyedArchiver , the class must conform to the NSCoding protocol. 我环顾四周,发现为了使用NSKeyedArchiver ,该类必须符合NSCoding协议。 I also found that if I was creating a custom class, I could make it conform to the NSCoding protocol by implementing encodeWithCoder: on such a custom class. 我还发现,如果我要创建一个自定义类,则可以通过在这样的自定义类上实现encodeWithCoder:使其符合NSCoding协议。

Essentially, I assume that in order to convert my EKEvent to NSData and back, I will need to create a custom class (let us call it CustomEvent ) I need to do the following: 本质上,我假设为了将EKEvent转换为NSData并返回,我需要创建一个自定义类(让我们将其称为CustomEvent ),我需要执行以下操作:

EKEvent --> CustomEvent --> NSData --> CustomEvent --> EKEvent

Can I get any help on learning how to create a custom class which DUPLICATES an existing class with the exception that I implement encodeWithCoder: to make it conform to NSCoding ? 我可以在学习如何创建一个自定义类中获得任何帮助吗,该自定义类会encodeWithCoder:现有类,但我要实现encodeWithCoder:使其符合NSCoding

I'm looking at EKEvent.h, and I know that it involves other classes which I must also duplicate (because they too don't conform to NSCoding ). 我正在查看EKEvent.h,我知道它涉及到其他类,我也必须重复这些类(因为它们也不符合NSCoding )。 Can anyone send me a tutorial link or help me out? 有人可以给我发送教程链接或帮我吗?

Thanks in advance! 提前致谢!

What you're describing appears to be a subclass . 您所描述的似乎是一个子类

However, in Objective-C, you have the simpler option of defining a category on an existing class to add the functionality you want. 但是,在Objective-C中,您可以使用更简单的选项在现有类上定义类别以添加所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM