Objective-C Default Argument Value

前端 未结 6 1750
广开言路
广开言路 2020-12-10 03:58

Hey there, quick question here. I\'m sure there\'s a simple answer.

Coming from PHP, I\'m used to declaring a function with a default argument value like this:

6条回答
  •  青春惊慌失措
    2020-12-10 04:18

    You can easily achieve the same effect using #define.

    The function in your header file:

    +(NSDate*)getDateFromYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day;
    

    Add a #define for parameter function in header file:

    #define GetDateFromYearOnly(year) [YourClassName getDateFromYear:year month:1 day:1]
    

    Then your can use the function like:

    NSDate* 2015Date = GetDateFromYearOnly(2015);
    

    And you will get an NSDate object with date 2015/01/01.

    If the function is not static, build a new function like this:

    -(NSDate*)GetDateFromYearOnly:(NSUInteger)year;
    

    And call:

    [self getDateFromYear:year month:1 day:1]
    

提交回复
热议问题