Saturday, July 19, 2014

Multiple arguments in objective-c explained

A small example:

#import 

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        // initialize a date with current date and time
        NSDate *now = [[NSDate alloc] init];
        NSLog(@"now is %@", now);
        double seconds = [now timeIntervalSince1970];
        NSLog(@"It has been %f seconds since 1970", seconds);
        NSDate *later = [now dateByAddingTimeInterval:100000];
        NSLog(@"later is %@", later);

        NSCalendar *cal = [NSCalendar currentCalendar];
        NSUInteger day = [cal ordinalityOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:now];
        NSLog(@"this is day %lu of the month", day);

    }
    return 0;
}

Note this line:

        NSUInteger day = [cal ordinalityOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:now];

From a c++ point of view, it means something like this:

    cal->ordinalityOfUnit:inUnit:forDate(NSDayCalendarUnit, NSMonthCalendarUnit, now);

It's really funny that you can actually split method names in objective-c! :D

0 comments: