正确地继承EKEvent类 [英] Properly subclassing the EKEvent Class

查看:82
本文介绍了正确地继承EKEvent类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对 EKEvent 类进行子类化时遇到了一些麻烦。方案是这样的,我使用webservice从外部数据库中提取所有事件,因此所有事件都带有ID。然后,我想将这些事件放入设备日历中,然后再检索它们。问题是,当我检索事件时,我需要它与服务器上的事件具有相同的ID,因此我可以快速查找以获得有关事件的其他信息。

I have been having a bit of trouble sublclassing the EKEvent Class. The scenario is this, I am pulling all my events from an external database using a webservice, so all the events come with an ID. I then want to put these events into the device calendar and retrieve them later. The problem is, when I retrieve the event I need it to have the same id as the event on the server so I can do a quick look up to get additional info on the event.

我知道 EKEvent 标识符属性是只读的,因此我想创建的原因该类的子类,我可以在其中添加一个名为 myid 的附加属性,并将事件的id(来自服务器的id)存储在eventstore中。我试图创建一个子类,一切似乎工作正常并编译,但在运行时我得到一个错误,当我尝试设置我在子类中添加的额外的eventid比例,错误消息是:

I am aware that the identifier property of EKEvent is read only, hence the reason I want to create a subclass of the class where I can add an additional property called something like myid and store the id of the event (the one from the server) with it in the eventstore. I have tried to create a subclass and everything seems to work fine and compiles, but on runtime I get an error when I try to set the extra eventid proporty that I add in the subclass, the error message is:

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[EKEvent setEventId:]: unrecognized selector sent to instance 0x83c0770'

这是我用来从我的 EKEvent subclass:

This is some test code I use to create the event from my EKEvent subclass:

 SectureEvent *myEvent  = (SectureEvent*)[EKEvent eventWithEventStore:eventDB];
 myEvent.title     = self.evento;
 myEvent.startDate = [[NSDate alloc] init];
 myEvent.startDate = [NSDate date];
 myEvent.endDate   = [[NSDate alloc] init];
 myEvent.endDate   = [[NSDate alloc] init];
 myEvent.allDay = YES;
 myEvent.eventId = self.eventId;

错误发生在最后一行 myEvent.eventId = self.eventId;
和app崩溃。所以我的问题是,如果我可以有效地继承 EKEvent 类,那么我在这里做错了什么?

The error occurs on the last line myEvent.eventId = self.eventId; and app crashes. So my question essentailly if I can effectively subclass the EKEvent class and if so what am I doing wrong here?

提前致谢!

推荐答案

EKEvent 并不打算进行子类化。 Event Kit对象用于表示数据库记录。创建 EKEvent 的子类不会在Event Kit数据库中神奇地插入新字段,也不会将 EKEvent 转换为某些内容否则神奇地改变该对象的类。

EKEvent is not meant to be subclassed. Event Kit objects are used to represent database records. Creating a subclass of EKEvent will not magically insert new fields in the Event Kit database, nor will casting a EKEvent to something else magically change that object' class.

将额外字段存储到数据库中的唯一方法是直接访问该数据库,Apple自行保留。

The only way to store extra fields into the database is to have a direct access to that database, which Apple reserves for themselves.

由于无法向Event Kit数据库添加新字段,您可以使用现有字段(例如,在事件的注释中添加事件ID)或扩展它由您的应用程序管理的第二个数据库。

As you cannot add new fields to the Event Kit database, you can either use the existing fields (for example, add the event ID in the notes of the event) or extend it with a second database managed by your application.

只需创建一个SQLite数据库(或属性列表文件,或任何您想要的格式),将您的事件ID与 EKEvent 标识符。

Just create a SQLite database (or a Property List file, or whatever format you want) that associate your event IDs to EKEvent identifiers.

这篇关于正确地继承EKEvent类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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