Challenge

Use two instances of NSDate to figure out how many seconds you have been alive.

First, NSDate has an instance method timeIntervalSinceDate:. This method takes one argument – another instance of NSDate. It returns the number of seconds between the NSDate that received the message and the NSDate that was passed in as the argument.

It looks something like this:

d​o​u​b​l​e​ ​s​e​c​o​n​d​s​S​i​n​c​e​E​a​r​l​i​e​r​D​a​t​e​ ​=​ ​[​l​a​t​e​r​D​a​t​e​ ​t​i​m​e​I​n​t​e​r​v​a​l​S​i​n​c​e​D​a​t​e​:​e​a​r​l​i​e​r​D​a​t​e​]​;​

Second, you will need to create a new date object that is set to a given year, month, etc. You will do this with the help of an NSDateComponents object and an NSCalendar object. Here is an example:

N​S​D​a​t​e​C​o​m​p​o​n​e​n​t​s​ ​*​c​o​m​p​s​ ​=​ ​[​[​N​S​D​a​t​e​C​o​m​p​o​n​e​n​t​s​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
[​c​o​m​p​s​ ​s​e​t​Y​e​a​r​:​1​9​6​9​]​;​
[​c​o​m​p​s​ ​s​e​t​M​o​n​t​h​:​4​]​;​
[​c​o​m​p​s​ ​s​e​t​D​a​y​:​3​0​]​;​
[​c​o​m​p​s​ ​s​e​t​H​o​u​r​:​1​3​]​;​
[​c​o​m​p​s​ ​s​e​t​M​i​n​u​t​e​:​1​0​]​;​
[​c​o​m​p​s​ ​s​e​t​S​e​c​o​n​d​:​0​]​;​

N​S​C​a​l​e​n​d​a​r​ ​*​g​ ​=​ ​[​[​N​S​C​a​l​e​n​d​a​r​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​C​a​l​e​n​d​a​r​I​d​e​n​t​i​f​i​e​r​:​N​S​G​r​e​g​o​r​i​a​n​C​a​l​e​n​d​a​r​]​;​
N​S​D​a​t​e​ ​*​d​a​t​e​O​f​B​i​r​t​h​ ​=​ ​[​g​ ​d​a​t​e​F​r​o​m​C​o​m​p​o​n​e​n​t​s​:​c​o​m​p​s​]​;​

Good luck!

..................Content has been hidden....................

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