Online Book Reader

Home Category

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

By Root 471 0
of characters in a string, you use the length method:

NSUInteger charCount = [x length];

And you check to see if two strings are equal using the isEqual: method:

if ([lament isEqual:x])

NSLog(@"%@ and %@ are equal", lament, x);

The C language also has a character string construct. Here’s what the preceding string examples would look like in C:

char *lament = "Why me!?";

char *x;

asprintf(&x, "The best number is %d", 5);

size_t charCount = strlen(x);

if (strcmp(lament, x) == 0)

printf("%s and %s are equal\n", lament, x);

free(x);

We will discuss C strings in Chapter 34. But whenever you have a choice, use NSString objects instead of C strings. The NSString class has many methods designed to make your life easier. Plus, NSString (which is based on the UNICODE standard) is exceedingly good at holding text from any language on the planet. Strange characters? No problem. Text that reads from right to left? No worries.

Challenge


There is a class called NSHost which has information about your computer. To get an instance of NSHost, you can use the NSHost class method currentHost. This method returns a pointer to an NSHost object:

+ (NSHost *)currentHost

To get the localized name of your computer, you can use the NSHost instance method localizedName that returns a pointer to an NSString object:

- (NSString *)localizedName

Write a Foundation Command Line Tool that prints out the name of your computer. (This sometimes takes a surprisingly long time to run.)

15

NSArray


Like NSString, NSArray is another class that Objective-C programmers use a lot. An instance of NSArray holds a list of pointers to other objects. For example, let’s say you want to make a list of three NSDate objects and then go through the list and print each date.

Create a new project : a Foundation Command Line Tool called DateList. Open main.m and change the main() function:

#import

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

{

@autoreleasepool {

// Create three NSDate objects

NSDate *now = [NSDate date];

NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];

NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];

// Create an array containing all three (nil terminates the list)

NSArray *dateList = [NSArray arrayWithObjects:now, tomorrow, yesterday, nil];

// How many dates are there?

NSLog(@"There are %lu dates", [dateList count]);

// Print a couple

NSLog(@"The first date is %@", [dateList objectAtIndex:0]);

NSLog(@"The third date is %@", [dateList objectAtIndex:2]);

}

return 0;

}

NSArray has two methods, shown in this example, that you will use all the time:

count returns the number of items in an array

objectAtIndex: returns the pointer that the array has stored at the index specified by the argument.

Arrays are ordered, and you access an item in the array by its index. Arrays are zero-based: the first item is stored at index 0, the second item is stored at index 1, and so on. Thus, if the count method says there are 100 items in the array, you can use objectAtIndex: to ask for the objects at indices 0 to 99.

Figure 15.1 is an object diagram of your program. Notice that the instance of NSArray has pointers to the NSDate objects.

Figure 15.1 Object diagram for DateList

Thus, if you want to loop and process each item in the array (or “iterate over the array”), you can create a for-loop. Edit main.m:

#import

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

{

@autoreleasepool {

// Create three NSDate objects

NSDate *now = [NSDate date];

NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];

NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];

// Create an array containing all three (nil terminates the list)

NSArray *dateList = [NSArray arrayWithObjects:now, tomorrow, yesterday, nil];

NSUInteger dateCount = [dateList count];

for (int i = 0; i < dateCount; i++) {

NSDate *d = [dateList objectAtIndex:i];

NSLog(@"Here is a date: %@",

Return Main Page Previous Page Next Page

®Online Book Reader