Graphics Programs Reference
In-Depth Information
'-[BNRItem doSomethingWeird]: unrecognized selector sent to instance 0x100117280'
This is what an exception looks like. What exactly is it saying? First it tells us the date,
time, and name of the application. You can ignore that information and focus on what
comes after the “***.” That line tells us that an exception occurred and the reason.
The reason is the most important piece of information an exception gives you. Here the
reason tells us that an unrecognized selector was sent to an instance. You know that se-
lector means message. You sent a message to an object, and the object does not implement
that method.
The type of the receiver and the name of the message are also in this output, which makes
it easier to debug. An instance of BNRItem was sent the message doSo-
methingWeird . The - at the beginning tells you the receiver was an instance of
BNRItem . A + would mean the class itself was the receiver.
Xcode did try to warn us that something bad might happen: check the issue navigator to
see the warning from the compiler that BNRItem has an incomplete implementation.
There are two important lessons to take away from this. First, always check the console if
your application halts or crashes; errors that occur at runtime (exceptions) are just as im-
portant as those that occur during compiling. Second, remember that unrecognized select-
or means the message you are sending isn't implemented by the receiver. And by remem-
ber, I mean write it down somewhere. You will make this mistake more than once, and
you'll want 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 pro-
grammer 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;
 
Search WWH ::




Custom Search