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​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.