Multiple arguments

Some methods take several arguments. For example, an NSDate object doesn’t know what day of the month it is. If you needed this information, you would use an NSCalendar object. The NSCalendar method that can tell you the day of the month takes three arguments. Create an NSCalendar object and use it in main.m:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​[​N​S​D​a​t​e​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​e​ ​d​a​t​e​ ​i​s​ ​%​@​"​,​ ​n​o​w​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​d​o​u​b​l​e​ ​s​e​c​o​n​d​s​ ​=​ ​[​n​o​w​ ​t​i​m​e​I​n​t​e​r​v​a​l​S​i​n​c​e​1​9​7​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​I​t​ ​h​a​s​ ​b​e​e​n​ ​%​f​ ​s​e​c​o​n​d​s​ ​s​i​n​c​e​ ​t​h​e​ ​s​t​a​r​t​ ​o​f​ ​1​9​7​0​.​"​,​ ​s​e​c​o​n​d​s​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​l​a​t​e​r​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​1​0​0​0​0​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​I​n​ ​1​0​0​,​0​0​0​ ​s​e​c​o​n​d​s​ ​i​t​ ​w​i​l​l​ ​b​e​ ​%​@​"​,​ ​l​a​t​e​r​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​C​a​l​e​n​d​a​r​ ​*​c​a​l​ ​=​ ​[​N​S​C​a​l​e​n​d​a​r​ ​c​u​r​r​e​n​t​C​a​l​e​n​d​a​r​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​U​I​n​t​e​g​e​r​ ​d​a​y​ ​=​ ​[​c​a​l​ ​o​r​d​i​n​a​l​i​t​y​O​f​U​n​i​t​:​N​S​D​a​y​C​a​l​e​n​d​a​r​U​n​i​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​n​U​n​i​t​:​N​S​M​o​n​t​h​C​a​l​e​n​d​a​r​U​n​i​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​D​a​t​e​:​n​o​w​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​i​s​ ​i​s​ ​d​a​y​ ​%​l​u​ ​o​f​ ​t​h​e​ ​m​o​n​t​h​"​,​ ​d​a​y​)​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

The method’s name is ordinalityOfUnit:inUnit:forDate:, and it takes three arguments. You can tell because the method name includes three colons. Notice that I split that message send into three lines. This is fine; the compiler does not mind the extra whitespace. Objective-C programmers typically line up the colons so that it is easy to tell the parts of the method name from the arguments. (Xcode should do this for you: every time you start a new line, the previous line should indent properly. If that isn’t happening, check your Xcode preferences for indention.)

The first and second arguments of this method are the constants NSDayCalendarUnit and NSMonthCalendarUnit. These constants are defined in the NSCalendar class. They tell the method that you want to know what day it is within the month. The third argument is the date you want to know about.

If you wanted to know the hour of the year instead of the day of the month, you’d use these constants:

 ​ ​ ​ ​N​S​U​I​n​t​e​g​e​r​ ​h​o​u​r​ ​=​ ​[​c​a​l​ ​o​r​d​i​n​a​l​i​t​y​O​f​U​n​i​t​:​N​S​H​o​u​r​C​a​l​e​n​d​a​r​U​n​i​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​n​U​n​i​t​:​N​S​Y​e​a​r​C​a​l​e​n​d​a​r​U​n​i​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​D​a​t​e​:​n​o​w​]​;​
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset