Cocoa Core Competencies

Value object

A value object is in essence an object-oriented wrapper for a simple data element such as a string, number, or date. The common value classes in Cocoa are NSString, NSDate, and NSNumber. Value objects are often attributes of other custom objects you create.

You can put any of the value objects in a collection, such as an instance of NSArray or NSDictionary.

Using NSString, and its subclass NSMutableString, you can perform a wide range of string-related operations. For example, you can join strings together, split strings apart, work on file paths, transform the case of characters, and search for substrings. In all of these, string objects are treated as Unicode.

Using NSDate, in conjunction with NSCalendar and other related classes, you can perform complicated calendrical calculations such as determining the number of months and days between two instants in time based on the user’s preferred calendar, taking into account variables such as time zones and leap years.

Using the NSNumber subclass NSDecimalNumber, you can accurately perform currency-based calculations.

NSValue

NSValue provides a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as char, int, float, or double, as well as pointers, structures, and object IDs. It lets you add items of such data types to collections such as instances of NSArray and NSSet, which require their elements to be objects. This is particularly useful if you need to put point, size, or rectangle structures (such as NSPoint, CGSize, or NSRect) into a collection.