Friday, July 10, 2009

Completed Pickers Tutorial



In this tutorial, I created a slot machine game using Tab Bars and Pickers. The game works by clicking the spin button, which will spin five reels at random. If three or more images line up in a row, a song is played and you win.

Although this tutorial was primarily meant to be an introduction to Tab Bars and Pickers, this tutorial also taught me how to display images and plays sounds programmatically.

For those interested in the tutorial, please see Chapter 7 in Dave Mark's "Beginning iPhone Development: Exploring the iPhone SDK"

How do I find EXC_BAD_ACCESS bugs in a Cocoa project?

Q: How do I find EXC_BAD_ACCESS bugs in a Cocoa project?

A: This kind of problem is usually the result of over-releasing an object. It can be very confusing, since the failure tends to occur well after the mistake is made. The crash can also occur while the program is deep in framework code, often with none of your own code visible in the stack.
Summary

To avoid problems like this, you must follow the Cocoa memory management rules. Refer to ADC's document "Memory Management Programming Guide for Cocoa". The section “Object Ownership and Disposal” describes the primary policy.

http://developer.apple.com/qa/qa2004/qa1367.html

Monday, July 6, 2009

Objective-C: When should you use @class instead of #import

Say you're looking at AppController.h, which contains this code:

#import
@class PreferenceController;

@interface AppController : NSObject
{

PreferenceController *preferenceController;
}

-(IBAction)showPreferencePanel:(id)sender;

If you are wondering why @class is used within AppController.h, Aaron Hillegass in "Cocoa(R) Programming for Mac(R) OS X (3rd Edition)" explains:

The "@class PreferenceController;" line tells the compiler that there is a class PreferenceController. You can then make the following declaration without importing the header file for PreferenceController:

PreferenceController *preferenceController;

You could replace

@class PreferenceController;

with

#import "PreferenceController.h"

The #import statement would import the header, and the compiler would learn PreferenceController was a class. Because the import command requires the compiler to parse more files, @class will often result in faster builds.