Created
August 26, 2012 09:02
-
-
Save mootoh/3476427 to your computer and use it in GitHub Desktop.
Convert EventKit object to NSDictionary, for making it easy to serialize in JSON format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Convert EventKit object to NSDictionary, for making it easy to serialize in JSON format. | |
// | |
#import "EventKit+Serializer.h" | |
@implementation EKCalendarItem (Serializer) | |
- (NSDictionary *)toDictionary | |
{ | |
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | |
dict[@"calendarItemIdentifier"] = self.calendarItemIdentifier; | |
dict[@"calendarItemExternalIdentifier"] = self.calendarItemExternalIdentifier; | |
dict[@"creationDate"] = [self.creationDate description]; | |
dict[@"lastModifiedDate"] = [self.lastModifiedDate description]; | |
if (self.timeZone) dict[@"timeZone"] = [self.timeZone description]; | |
if (self.URL) dict[@"URL"] = [self.URL description]; | |
dict[@"title"] = self.title; | |
if (self.location) dict[@"location"] = self.location; | |
if (self.hasNotes) dict[@"notes"] = self.notes; | |
// alarms | |
if (self.hasAlarms) { | |
NSMutableArray *alarms = [NSMutableArray arrayWithCapacity:self.alarms.count]; | |
for (EKAlarm *alarm in self.alarms) { | |
NSDictionary *alarmDict = @{@"absoluteDate": [alarm.absoluteDate description], @"relativeOffset":[NSNumber numberWithDouble:alarm.relativeOffset]}; | |
[alarms addObject:alarmDict]; | |
} | |
dict[@"alarms"] = alarms; | |
} | |
// attendees | |
if (self.hasAttendees) { | |
// TODO | |
} | |
// recurrence | |
if (self.hasRecurrenceRules) { | |
NSMutableArray *rules = [NSMutableArray arrayWithCapacity:self.recurrenceRules.count]; | |
for (EKRecurrenceRule *rule in self.recurrenceRules) { | |
[rules addObject:[rule toDictionary]]; | |
} | |
dict[@"recurrenceRules"] = rules; | |
} | |
return dict; | |
} | |
@end | |
@implementation EKCalendar (Serializer) | |
- (NSDictionary *)toDictionary | |
{ | |
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | |
dict[@"calendarIdentifier"] = self.calendarIdentifier; | |
dict[@"title"] = self.title; | |
return dict; | |
} | |
@end | |
@implementation EKReminder (Serializer) | |
- (NSDictionary *)toDictionary | |
{ | |
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[super toDictionary]]; | |
NSCalendar *calendar = [NSCalendar currentCalendar]; | |
dict[@"startDateComponents"] = [[calendar dateFromComponents:self.startDateComponents] description]; | |
dict[@"dueDateComponents"] = [[calendar dateFromComponents:self.dueDateComponents] description]; | |
dict[@"completionDate"] = [self.completionDate description]; | |
return dict; | |
} | |
@end | |
@implementation EKRecurrenceRule (Serializer) | |
- (NSDictionary *)toDictionary | |
{ | |
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | |
dict[@"frequency"] = [NSNumber numberWithInt:self.frequency]; | |
dict[@"interval"] = [NSNumber numberWithInteger:self.interval]; | |
return dict; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment