O'Reilly logo

Cocoa® Programming for Mac OS® X For Dummies® by Erick Tejkowski

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required