Friday, July 10, 2009

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.

Wednesday, June 24, 2009

A way to display all possible typeahead values in XCode

As you enter code into the XCode IDE, the IDE will often attempt to complete the term for you by using a typeahead feature. To view all possible values that match the type-ahead, simply press the escape key when a partial term is displayed.




From Dave Mark's "Beginning iPhone Development: Exploring the iPhone SDK"

Monday, June 22, 2009

A shortcut to perform a keyword look-up within the XCode IDE

To look up a constant, variable, method or function in iPhone's documentation library diretly from within the XCode IDE, simply option+double-click on the term.

From Dave Mark's "Beginning iPhone Development: Exploring the iPhone SDK"

Sunday, June 21, 2009

Differences between Cocoa and Cocoa Touch

I learned...

According to Dave Mark in "Beginning iPhone Development: Exploring the iPhone SDK":

"If you are coming to iPhone with previous Cocoa experience, a few tools that you're probably accustomed to using aren't available on the iPhone. The iPhone SDK doesn't support Core Data or Cocoa Bindings. We mentioned earlier that Cocoa Touch uses Objective C 2.0, but one of the key new features of that language is not available on iPhone: Cocoa Touch does not support garbage collection."

Its a good thing I spent a lot of time reviewing memory management. It looks like those retain and release statements will come in handy. I'm also glad I stopped at Chapter 8 in "Cocoa(R) Programming for Mac(R) OS X (3rd Edition)" and picked up this new book. Core Data was only a few chapters away and I would have wasted lots of precious time learning something I wouldn't be able to use.

Currently reading "Beginning iPhone Development: Exploring the iPhone SDK" by Dave Mark & Jeff LaMarche



This Purchase: $26.39 | Running Total: $3008.11

The Benefit of Key-Value Coding

I am learning about Key-Value coding in Chapter 7 of "Cocoa(R) Programming for Mac(R) OS X (3rd Edition)".

I would say the most important lesson from this chapter is knowing what accessor methods are and when they get called. The author's tutorial in this chapter clearly demonstrates this behavior. In the tutorial, I implemented an AppController with two accessor methods "set" and "get".

The implementation of these methods were fundamentally simply.

"set" sets an instance variable in the AppController class to a specified value
"get" returns that value

The "set" accessor method got called when I used "setValue ... forKey".
e.g. [self setValue:[NSNumber numberWithInt:5] forKey:@"fido"];

The "get" accessor method got called when I used "valueForKey".
e.g. NSNumber *n = [self valueForKey:@"fido"];

The tutorial helped illustrate this by having me issue NSLog calls from within each accessor method. I was also taught how to bind key-value code to a graphical object, in this case a slider. When the slider moved, the NSLog calls showed the set and get methods being called in the GDB debug console window.