如何从已存在的类创建自定义类? (现有类不支持NSCoding)
我想做的是将 EKEvent
转换为 NSData
,然后将其转换回 EKEvent
。
我环顾四周,发现为了使用 NSKeyedArchiver,该类必须符合 NSCoding 协议。我还发现,如果我要创建自定义类,我可以通过在此类自定义类上实现 encodeWithCoder:
来使其符合 NSCoding
协议。
本质上,我假设为了将我的 EKEvent
转换为 NSData
并返回,我需要创建一个自定义类(让我们称之为 CustomEvent
>)我需要执行以下操作:
EKEvent --> CustomEvent --> NSData --> CustomEvent --> EKEvent
我可以获得有关学习如何创建重复现有类的自定义类的任何帮助,但我实现 encodeWithCoder:
以使其符合 NS编码
?
我正在查看 EKEvent.h,我知道它涉及其他类,我也必须复制这些类(因为它们也不符合 NSCoding
)。有人可以给我发送教程链接或帮助我吗?
提前致谢!
What I'm trying to do is convert an EKEvent
into NSData
, and then convert it back into an EKEvent
.
I looked around and noticed that in order to use NSKeyedArchiver
, the class must conform to the NSCoding
protocol. 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.
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 --> 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
?
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
). Can anyone send me a tutorial link or help me out?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的似乎是 子类。
但是,在 Objective-C 中,您可以更简单地选择定义 现有类的类别 以添加您想要的功能。
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.