to be able to diagnose it quickly.
Some languages use try and catch blocks to handle exceptions. While Objective-C has this ability, we don’t use it very often in application code. Typically, an exception is a programmer error and should be fixed in the code instead of handled at runtime.
Before continuing, remove the exception-causing code: first from main.m ...
for (int i = 0; i < 10; i++) {
BNRItem *p = [BNRItem randomItem];
[p doSomethingWeird];
[items addObject:p];
}
and then from BNRItem.h ...
- (void)doSomethingWeird;
Fast Enumeration
Before Objective-C 2.0, we iterated through arrays the way you did in your main function:
for (int i = 0; i < [items count]; i++) {
BNRItem *item = [items objectAtIndex:i];
NSLog(@"%@", item);
}
Objective-C 2.0 introduced fast enumeration . With fast enumeration, you can write that code segment much more succinctly. Make the following change in main.m :
for (int i = 0; i < [items count]; i++) {
BNRItem *item = [items objectAtIndex:i];
NSLog(@"%@", item);
}
for (BNRItem *item in items) {
NSLog(@"%@", item);
}
items = nil;
In this chapter, we have covered the basics of Objective-C. In the next chapter, we will discuss memory management in Cocoa Touch.
Challenges
Most chapters in this book will finish with at least one challenge that encourages you to take your work in the chapter one step further and prove to yourself what you’ve learned. We suggest that you tackle as many of these challenges as you can to cement your knowledge and move from learning iOS development from us to doing iOS development on your own.
Challenges come in three levels of difficulty:
Bronze challenges typically ask you to do something very similar to what you did in the chapter. These challenges reinforce what you learned in the chapter and force you to type in similar code without having it laid out in front of you. Practice makes perfect.
Silver-level challenges require you to do more digging and more thinking. You will need to use methods, classes, and properties that you haven’t seen before. But the tasks are still similar to what you did in the chapter.
Gold challenges are difficult and can take hours to complete. They require you to understand the concepts from the chapter and then do some quality thinking and problem-solving on your own. Tackling these challenges will prepare you for the real-world work of iOS development.
Before beginning any challenge: always make a copy of your project directory in Finder and attack the challenge in that copy . Many chapters build on previous chapters, and working on challenges in a copy of the project assures you will be able to progress through the book.
Bronze Challenge: Bug Finding
Create a bug in your program by asking for the eleventh item in the array. Run it and note the exception that gets thrown.
Silver Challenge: Another initializer
Create another initializer method for BNRItem . This initializer is not the designated initializer of BNRItem . It takes an NSString that identifies the itemName of the item and an NSString that identifies the serialNumber .
Gold Challenge: Another Class
Create a subclass of BNRItem named BNRContainer . A BNRContainer should have an array of subitems that contains instances of BNRItem . Printing the description of a BNRContainer should show you the name of the container, its value in dollars (a sum of all items in the container plus the value of the container itself), and a list of every BNRItem it contains. A properly-written BNRContainer can contain instances of BNRContainer . It can also report back its full value and every contained item properly.
Are You More Curious?
In addition to Challenges, many chapters will conclude with one or more “ For the More Curious ” sections. These sections offer deeper explanations of or additional information about