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

发布于 2024-09-08 20:25:59 字数 676 浏览 7 评论 0原文

我想做的是将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-09-15 20:25:59

您所描述的似乎是 子类

但是,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文