outlet - instance variable that points to another object (usually a UI Control).

actions - methods that can be triggered by user interaction objects.

IBAction - Same as void. (used to give hints to xcode and Interface Builder).

id - can point to any type.

nil is the null type (Cocoa NSNull for Arrays).

Booleans:

YES, NO, TRUE, FALSE

Classes

All methods public

By default, all instance variables are protected (which most developers use).

NSString - @"this is an NSString"

To do a static class property in Objective-C, declare it in .m file outside of @implementation.

static BOOL foo;

then access it from within the class.

foo = TRUE;

If you need access to it outside of the class, create accessor methods around it.

You can also extern it to make it global / public

extern BOOL foo;

Example here:
http://www.omnigroup.com/mailman/archive/macosx-dev/2002-April/037869.html

You can have multiple initializers for a class, but one should be designated as the main one (usually the one with the most arguments) and all other intializers go through it.

Note, you should do:

-(id)init

{

if(![super init])

{

return nil;

}

return self;

}

 


in your main initializer incase the super class cant be initializer (rare but possible).


Can set debugger to open on any exceptions thrown. You have to add breakpoint for it though, by adding a symbolic breakpoint for obj_exception_throw. See page 60 of "Cocoa programming for Mac OS X).


Can also use NSAssert to throw an exception if certain conditions are met.


You can setup Release / Debug builds to ignore the asserts as makes sense.


Garbage Collection

---------------------------


Automatic Garbage Collection

------


OS 10.5 has support for automatic Garbage collection. You enable it in the Build properties for the application target. If you use this, you do not have to worry about retain, release or autorelease.


Just make sure to set objects / variables to nil when you are done using them.


Manual Garbage Collection

------


Required for development for OS X prior to 10.5 and for iPhone.


If you create an object using the manual alloc style, you need to releasethe object later.


So the only real work is managing local references inside a function. And there's only one rule: if you create an object with alloc or copy, send it arelease or autorelease message at the end of the function. If you create an object any other way, do nothing.


When putting an object into an Array, array calls retain on it, so if there are not other references, make sure to release it outside of the array.


When retain count is 1 and release is called, and objects dealloc method is called (which is used to clean up the object references).


-(void)dealloc

{

[super dealloc]

}

 



alloc always leads to a retain +1

autorelease
-------

Allows a retain to be made on an object, and then released later (even when the original no longer has a reference to the object.)

Rules:

-objects created by alloc, new, copy or mutableCopy havea  retain count of 1 and are not in the autorelease pool.
-if you get an object from any other methods, assume it has a retain count of 1 and is in the autorelease pool. if you dont want it to be deallocated, then call retain on it.

some classes have methods that return autoreleased objects.

----------

note, when using accesor methods, make sure to retain the new value, release the old and then set the accessor setter.

Getter / Setter

do:

setFoo
foo

dont:

setFoo
getFoo

target / actions
----------------

Can programatically set the action of a NSControl

Does each control just have one action? i.e. for a button, its action is when it is clicked?


--------------
Close App when window is closed:

Add this delegate for the window.

-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
 return YES;
}
 Then:

[NSApp setDelegate:self];


When setting delegates for a control, you usually have to do it in awakeFromNib and not init, as the control has not been loaded yet when init runs.

------
NSButton setKeyEquivalent

[startButton setKeyEquivalent:@"\r"];

simulates button click when specified char / key is pressed.

-------

set window size:

NSRect newFrame = [window frame];

newFrame.size = NSMakeSize(400, 200);

[window setFrame:newFrame display:NO];



-----


load file into string from resources dir


NSString *data = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ptswtab" ofType:@"txt"]];

 


-------


create a custom scharacterset


NSMutableCharacterSet *workingSet;

NSCharacterSet *finalCharSet;

workingSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];

[workingSet addCharactersInString:@"\""];

finalCharSet = [workingSet copy];

[workingSet release];

NSString *data = [self stringByTrimmingCharactersInSet:finalCharSet];

 



-----
Turning off Xcode's "Undo past save" warning
http://www.borkware.com/quickies/single?id=386

-----

Creating a bottom bar for leopard

add in awakeFromNib

[[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
[[self window] setContentBorderThickness: 32.0 forEdge: NSMinYEdge];