Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [48]

By Root 431 0

NSLog(@"Giving up ownership of arrays");

allAssets = nil;

employees = nil;

}

return 0;

}

Build and run the program. The executives dictionary should log itself out:

executives = {

CEO = "";

CTO = "";

}

Figure 21.3 Two instances of Employee in an NSMutableDictionary

C primitive types


The collections covered in this chapter only hold objects. What if you want a collection of floats or ints or pointers to structures? You can wrap common C primitive types in an object. There are two classes that are designed specifically for this purpose. NSNumber holds C number types. NSValue can hold a pointer and some types of structs.

For instance, if you wanted to put the numbers 4 and 5.6 into an array, you would use NSNumber:

NSMutableArray *numList = [[NSMutableArray alloc] init];

[numList addObject:[NSNumber numberWithInt:4]];

[numList addObject:[NSNumber numberWithFloat:5.6]];

Collections and nil


You are not allowed to add nil to any of the collection classes we have covered. What if you need to put that idea of nothingness, a “hole,” into a collection? There is a class called NSNull. There is exactly one instance of NSNull, and it is an object that represents nothingness. Here’s an example.

NSMutableArray *hotel = [[NSMutableArray alloc] init];

// Lobby on the ground floor

[hotel addObject:lobby];

// Pool on the second

[hotel addObject:pool];

// The third floor has not been built out

[hotel addObject:[NSNull null]];

// Bedrooms on fourth floor

[hotel addObject:bedrooms];

Challenge


Explore the reference pages for NSArray, NSMutableArray, NSDictionary, and NSMutableDictionary. You will use these classes every day.

22

Constants


We have spent a lot of time discussing variables, which as the name indicates, change their values as the program runs. There are, however, pieces of information that do not change value. For example, the mathematical constant π never changes. We call these things constants, and there are two common ways that Objective-C programmers define constants: #define and global variables.

In Xcode, create a new Foundation Command Line Tool called Constants.

In the standard C libraries, constants are defined using the #define preprocessor directive. The math part of the standard C library is declared in the file math.h. One of the constants defined there is M_PI. Use it in main.m:

#import

int main (int argc, const char * argv[])

{

@autoreleasepool {

// In literal NSString, use \u for arbitrary unicode chars

NSLog(@"\u03c0 is %f", M_PI);

}

return 0;

}

When you build and run it, you should see:

π is 3.141593

To see the definition, Command-click on M_PI in the editor. It will take you to the following line in math.h:

#define M_PI 3.14159265358979323846264338327950288

Preprocessor directives


Compiling a file of C, C++, or Objective-C code is done in two passes. First, the preprocessor runs through the file. The output from the preprocessor then goes into the real compiler. Preprocessor directives start with #, and the three most popular are #include, #import, and #define.

#include and #import


#include and #import do essentially the same thing: request that the preprocessor read a file and add it to its output. Usually, you are including a file of declarations (a .h file), and those declarations are used by the compiler to understand the code it is compiling.

What is the difference between #include and #import? #import ensures that the preprocessor only includes a file once. #include will allow you to include the same file many times. C programmers tend to use #include. Objective-C programmers tend to use #import.

When specifying the name of the file to be imported, you can wrap the filename in quotes or angle brackets. Quotes indicate that the header is in your project directory. Angle brackets indicate that the header is in one of the standard locations that the preprocessor knows about. (, for example, is

Return Main Page Previous Page Next Page

®Online Book Reader