1

Can any one just tell me what the below code in iOS mean? Its a piece of code from MBCalendarKit.

@property (nonatomic, strong) NSMutableDictionary *data;

- (NSArray *)calendarView:(CKCalendarView *)calendarView eventsForDate:(NSDate *)date
{
    return [self data][date];
}

How does the above function return an NSArray when the global data is an NSMutableDictionary and the local date is NSDate?

And what does [self data][date] mean?

Moshe
  • 57,511
  • 78
  • 272
  • 425
Leo Joseph
  • 69
  • 1
  • 1
  • 8
  • Maybe something to do with short-hand synhtax: http://clang.llvm.org/docs/ObjectiveCLiterals.html ... – Larme May 22 '14 at 13:52

1 Answers1

7

If data is an NSDictionary then it means exactly the same thing as [data objectForKey:date]. That syntax was introduced a couple of years ago — ideally read the whole thing but if in a hurry then jump to 'Object Subscripting'.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • 1
    Interesting syntax, definitely creates a WTF moment which is not a good thing. For readability/understandability it would be best to use a different syntax or an intermediate for `[self data]`. Even `self.data[date]` would be an improvement. – zaph May 22 '14 at 14:00
  • 1
    As a side note, using an NSDate as the key here is valid because you can use any id (an object that conforms to the NSCopying protocol) as a key in a dictionary. – Kaan Dedeoglu May 22 '14 at 14:01
  • Thanks.. the syntax is now clear. But in this case does the NSDictionary is casted to NSArray? – Leo Joseph May 22 '14 at 14:14
  • 1
    It makes more sense if you name you dictionaries appropriately. Something like `self.dataItemsByDate[date];` – CrimsonChris May 22 '14 at 14:16
  • @LeoJoseph There is no casting going on here. Many languages support dictionary access with the square brackets. – CrimsonChris May 22 '14 at 14:57