1

i would like to add extra data to EKEvent, i tried NSDictionary (there is a lot of data to add) but it doesn't work.. sample code:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[eventStore setValue:dictionary forKey:MAIN_DICTIONARY];

any ideas?

1
  • What do you mean by "extra data"? What purpose? Analogy to Calendar?
    – qwerty_so
    Jul 22, 2015 at 10:21

2 Answers 2

0

You're using setValue:forKey: in a wrong way. That a look here. There are different options to achieve what you want: category, subclassing or create a class that contains the EKEvent and the NSMutableDictionary. It depends on how you need to use the EKEvent.

3
  • Categories are no solution, because they cannot define ivars. He would need associated objects for that. But see my answer. He cannot use subclassing, because such system data is typically instantiated by the system. So usually you do not have the ability, to say the system, what kind of class it should use creating the instance. Jul 22, 2015 at 8:43
  • Yes you're right. I was thinking about a category with associated objects! Jul 22, 2015 at 8:44
  • That is no good idea, because the event instances can be recreated inside the system without handling your additional information. Jul 22, 2015 at 8:52
0

You cannot do it this way, because even with key-value coding you can only set (declared or non declared) properties known by the instance. Basically the accessors (setter, getter) are executed. But there is no property MAIN_THREAD,no setter setMAIN_THREAD: in EKEvent.

If you want to extend instances of a foreign class that are created by the system (the instances, not the class), there are to ways to add data:

  1. You create an own class, let's say MyEvent and give them a reference to the system instance (EKEvent) as a property plus the properties you need. When you get an instance of EKEvent you look-up your list of MyEventss using the identifier. With that you have the full access to your data.

  2. You use associated objects. But you have to take care that they are not handled by the instance, i. e. while copying.

The first solution is better by far. Simple sample code:

@interface MyEvent : NSObject
@property (readonly) EKEvent* systemEvent;
@property id customProperty;

- (instancetype)eventForSystemEvent:(EKEvent*)systemEvent;
@end

@implemenation MyEvent

// Look-Up
NSMutableDictionary *eventLookUp;
+ (void)initialize
{
   if( self == [MyEvent class])
   {
     eventLookUp  = [NSMutableDictionary new];
   }
}

- (instancetype)eventForSystemEvent:(EKEvent*)systemEvent
{
  return eventLookUp[systemEvent.calendarItemIdentifier];
}

// Instance creation
- (instancetype)initWithSystemEvent:(EKEvent*)systemEvent
{
  // Usual initializer
  …
  eventLookUp[systemEvent.calendarItemIdentifier] = systemEvent;
  return self;
}

+ (instancetype)newEventWithSystemEvent:(EKEvent*)systemEvent
{
  return [[self alloc] initWithSystemEvent:systemEvent];
}
@end

Typped in Safari

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.