Online Book Reader

Home Category

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

By Root 501 0
arguments, the first thing you should do is make sure that the number of arguments is correct. Edit main.m:

#include

#include // atoi()

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

{

if (argc != 3) {

fprintf(stderr, "Usage: Affirmation \n");

return 1;

}

int count = atoi(argv[2]);

for (int j = 0; j < count; j++) {

printf("%s is cool.\n", argv[1]);

}

return 0;

}

atoi() is a standard C function that reads a C string and tries to make an int out of it.

Build and run the program.

37

Switch Statements


It is not uncommon to check a variable for a set of values. Using if-else statements, it would look like this:

int yeastType = ...;

if (yeastType == 1) {

makeBread();

} else if (yeastType == 2) {

makeBeer();

} else if (yeastType == 3) {

makeWine();

} else {

makeFuel();

}

To make this sort of thing easier, C has the switch statement. The code above could be changed to this:

int yeastType = ...;

switch (yeastType) {

case 1:

makeBread();

break;

case 2:

makeBeer();

break;

case 3:

makeWine();

break;

default:

makeFuel();

break;

}

Notice the break statements. Without the break, after executing appropriate case clause, the system would execute all the subsequent case clauses. For example, if I had this:

int yeastType = 2;

switch (yeastType) {

case 1:

makeBread();

case 2:

makeBeer();

case 3:

makeWine();

default:

makeFuel();

}

The program would run makeBeer(), makeWine(), and makeFuel(). This is primarily so that you can have multiple possible values trigger the same code:

int yeastType = ...;

switch (yeastType) {

case 1:

case 4:

makeBread();

break;

case 2:

case 5:

makeBeer();

break;

case 3:

makeWine();

break;

default:

makeFuel();

break;

}

As you can imagine, forgetting to put the break at the end of the case clause is a common programmer error, and it is only discovered when your program starts acting strangely.

In C, switch statements are for a very specific situation: the case can only be a constant integer. As such, you don’t see a lot of switch statements in most Objective-C programs. Which is why I snuck it in here just before the book ends.

Next Steps


Well, that’s everything you will ever need to know to write brilliant applications for iOS and Mac OS X.

That’s what I wish I could tell you. I know you have worked hard to get to this point.

The truth is that you have completed the first leg of a fun and rewarding journey. It is, however, a very long journey. It is now time for you to spend some time studying the standard frameworks that Apple makes available to Objective-C developers like you.

Let me repeat that last phrase so you can relish it: “Objective-C developers like you.” Congratulations.

If you are learning to develop applications for iOS, I recommend that you work through iOS Programming: The Big Nerd Ranch Guide, but there are several other books on iOS, and you are ready for any of them.

If you are learning to develop applications for Mac OS X, I recommend that you work through Cocoa Programming for Mac OS X, but, here again, there are several other books on Cocoa, and you are ready for any of them.

There are groups of developers who meet every month to discuss the craft. In most major cities, there are iOS Developers Meetups and CocoaHeads chapters. The talks are often surprisingly good. There are also discussion groups online. Take some time to find and use these resources.

Shameless plugs

You can find me on Twitter, where I post news about Big Nerd Ranch: @AaronHillegass

Keep an eye out for future guides from Big Nerd Ranch. We also offer week-long courses for developers. And if you just need some code written, we do contract programming. For more information, visit our website at www.bignerdranch.com.

It is you, dear reader, who makes my life of writing, coding, and teaching possible. So thank you for buying my book.

Index


* * *

A B C D E F G H I K L M N O P Q R S T U V W X Z

* * *

Symbols

! (logical NOT) operator, if/else

Return Main Page Previous Page Next Page

®Online Book Reader