2

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!

Nicolas Bachschmidt
  • 6,475
  • 2
  • 26
  • 36
Sparq
  • 823
  • 6
  • 13

2 Answers2

17

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.

Nicolas Bachschmidt
  • 6,475
  • 2
  • 26
  • 36
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];

jtomschroeder
  • 1,034
  • 7
  • 11
  • Thanks that worked and avoided the crash, I am however having problems retrieving the event from the event store. Is i possible to achieve what I am trying to do, when I get the events out of the event store using a predicate and put them into an array they are EKEvents, which lack the extra id property, can you think of a way around that? – Sparq Jul 09 '12 at 19:28
  • Having an OO solution is usually preferable, but would require some work to get what you want. I would recommend just using EKEvents and an associative array (or database depending on the anticipated number of events) to pair the ID from the server with the ID of the event. – jtomschroeder Jul 09 '12 at 19:42