Archive for October, 2010

One of the more frustrating aspects of writing for iDevices are the wasted days spent looking for the causes of some small, but show-stopping bugs.

For example, I’m currently developing a class with an OpenGL view (which is like a window into which I can draw 2D or 3D graphics) which is hot-swappable with my existing UIScrollView, so that either can be used to display and scroll the game’s background.

The problem was that the OpenGL view wasn’t registering any touches to the iDevice’s screen, and checking for the usual suspects, (user-interaction and multi-touch was enabled, the view was made first responder to events, etc.) yielded no joy.

I then compared my code with a previous incarnation of Animal Tracker which also used OpenGL, and also with a fresh copy of Apple’s own OpenGL template app, and nothing obvious seemed to be out of place.

I finally decided to create a new view class from scratch, checking that it could receive touches, and then copied across my non-working OpenGL view code verbatim, checking that touch events were no longer received. I then systematically removed chunks of code, checking for touch events after each excision, until eventually the touches were registered.

Putting back the just-excised code, I cut out just a small portion of it and checked for touches again. Nope, no touches registered. So, I cut out some more. Yep, touches registered. Continuing this process enabled me to home in on the culprit… a single line of code… a single statement.

In order for my UIScrollView and my OpenGL view to be interchangeable, I had created a protocol which basically just ensures that both views will respond to a minimum set of ‘scrolly’ type messages.

Unfortunately, I had forgotten that when I first created the OpenGL view class, I had put a temporary line of code in my ‘- (float) alpha’ method so that the program would compile, and this method just returned the dummy value ‘0.0’. But this basically means that if another part of the program wants to know how transparent the view is, it just tells them that the view is fully transparent – in other words, invisible!

Now, oddly, the graphics part of the operating system doesn’t read the ‘alpha’ method at all and displays the view regardless, but the touch response part does read it and, seeing that the view was saying it was invisible (and effectively not there), it didn’t bother to record any touches.

So, in a sense, my OpenGL view was both visible and invisible at the same time. D’oh!

Read Full Post »

Nilify or Nillify?

I have a dilemma. I was recently adding a comment to some code of mine when I discovered I needed to use a word which doesn’t exist. But before I get to that, I need to give a little background.

All computer languages have the concept of ‘variables’, which are just references to regions of memory that contain useful values. They are called ‘variables’ because the values they contain may vary throughout the running of the program. (This is in contrast to ‘constants’ whose values, once set, cannot be changed.)

For example, the variable called ‘numberOfCats’ may contain the value ‘5’. So, every time we refer to ‘numberOfCats’, we’re actually getting the last value that was stored in the ‘numberOfCats’ memory region, which in this case is ‘5’.

A ‘pointer’ is a variable which doesn’t contain a final value itself, but rather the address of the region of memory where another variable is located.

So, we could create a pointer called ‘animalCount’ that can be set to point to one of any number of animal counting variables, be it ‘numberOfCats’, ‘numberOfDogs’, ‘numberOfUnicorns’, etc, depending on the circumstances. This is useful because rather than duplicating much of the code each time a new animal is added to the program, a single section of code can use (and modify) the count of any animal variable that is passed to it.

But, what if the circumstance arises where we have to run that section of code, but we don’t have an animal count variable to pass to it? We have to pass something because the code is expecting it, so what do we pass?

Well, in some languages, such as ‘C’ and ‘C++’, we would set the animalCount pointer to ‘null’. So, the action of setting it to ‘null’ would be to ‘nullify’ it.

However, in Objective-C, we would set the animalCount pointer to ‘nil’. So, the action of setting it to ‘nil’ would be to… ‘nilify’ or ‘nillify’ it?

And that is the dilemma. Do I spell it with one ‘L’ or two?

Read Full Post »