Chapter 10. Cocoa Data Types

In This Chapter

  • Using the NSNumber class

  • Working with an array

  • Understanding Cocoa's Boolean type

  • Finding out what day it is

One of the main tasks that computers do well is manipulate data. Whether you need to calculate the national debt or keep track of the telephone numbers of the players in your poker club, computer programs can ease the task of handling information. Cocoa improves upon the various ways that you work with data in C.

This chapter takes you on a tour of some data types unique to Cocoa. The Foundation Framework defines these data types. You can continue to use the data types that you know from C, but after you see what Cocoa has to offer, you just might stop thinking about those old C types altogether.

Working with Numbers

When you program in Objective-C, you can use all the usual standard C numerical data types for making calculations. If you want to use these values with Cocoa's array objects, however, you're left out in the cold. For this reason, Cocoa offers you the NSNumber class. One of its great uses is to wrap numbers in a Cocoa object, for use with other objects, such as NSArray. The NSArray class is an array class that comes with the Foundation Kit. You hear more about it later in this chapter.

To create an NSNumber object based on an integer value of 42, use code like this:

NSNumber *n = [NSNumber numberWithInt:42];

Here are methods for creating NSNumbers based on a variety of numerical types:

  • numberWithDouble

  • numberWithFloat

  • numberWithInt ...

Get Cocoa® Programming for Mac OS® X For Dummies® 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.