问题
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.
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'
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;
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?
Thanks in advance!
回答1:
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.
The only way to store extra fields into the database is to have a direct access to that database, which Apple reserves for themselves.
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.
Just create a SQLite database (or a Property List file, or whatever format you want) that associate your event IDs to EKEvent
identifiers.
回答2:
Creating an EKEvent and casting it as a SectureEvent is not the same as creating a SectureEvent.
Try this:
SectureEvent *myEvent = [SectureEvent eventWithEventStore:eventDB];
来源:https://stackoverflow.com/questions/11401309/properly-subclassing-the-ekevent-class