Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
304 Using the Foundation Framework In this chapter, I introduce you to the NSDate class, which is used for constructing and manipu- lating data objects within your applications. I also introduce you to the NSCalendar class that allows you to specify the rules that are used in calculating dates. Finally, I'm going to introduce you to the NSDateFormatter class, which allows you to convert a date value into something that you can display to the user. These three classes, when used together, form an effective tool kit for all of your date handling needs. Using them is the preferred method of manipulating dates in Objective-C applications. Constructing Dates NSDate is the class that encapsulates a given instant in time. It includes both the date and time. It can be used to represent the current time, by constructing a new NSDate object by using the class method +date, or you can construct an NSDate object representing any time in the past or future by constructing one using an NSTimeInterval. Listing 15.1 shows how to create an NSDate object representing the current time. It actually shows two different meth- ods, the first using the +date factory method and the second using the standard initializer. Listing 15.1 Constructing an NSDate object