You are on page 1of 1

Objective-C Reference Card Protocols (like Java Interfaces) Memory Management 101

(for Java Programmers) If an object calls alloc, copy, or retain, it must also even-
MyProtocol.h: tually release the object (perhaps in another method, or at
@protocol MyProtocol least in its -dealloc method.
Basic Syntax - (void) aProtocolMethod; autorelease performs a release some time after the calling
- (void) anotherProcotolMethod; method has exited. It allows the calling method to use (or
All of C syntax is inherited by Objective C with the following @end
additions: store and retain) the object before the release happens.
A class that adopts a protocol would do the following: If any method stores a pointer to an object internally, it must
Declare objectx to be a pointer to an object of type MyClass,
#import "MyProtocol.h" retain that object until that pointer is cleared. Care must
allocate and initialize it:
@interface ClassName : ItsSuperclass < MyProtocol, be taken when objects have circular references.
MyClass *objectx = [[MyClass alloc] init]; AnotherProtocol > Accessor methods pattern:
Invoke methodf of objectx, no args [objectx methodf]; // method declarations -(NSString *) getAttr {
Invoke methodg of objectx, passing arg1 @end return attr;
}
[objectx methodg: arg1]; -(void) setAttr:(NSString *)newAttr {
Methods with more than one argument:
Categories (extend any class)
id oldAttr = attr;
[objectx initWithData: myData andParent: myParent]; attr = [newAttr retain];
If you want to extend any class in the system, in this exam-
[oldAttr release];
ple ClassName, first define a category in a header file. Note: a
}
category cannot define any instance variables, just new meth-
Header File ods. Note: it’s a good idea to have accessors for all instance
variables. It helps with memory management as well as
MyClass.h: CategoryName.h: Key/Value encoding. Thus, even though Objective C lets you
@interface MyClass : MySuperClass #import "ClassName.h" declare @public, @private and @protected instance vari-
{ @interface ClassName ( CategoryName ) ables, external classes should always use accessors instead
int instanceVar1; // method declarations of directly modifying them. Oddly, there’s no way to declare
NSString *instanceVar2; @end private methods. See Categories’ Note2 to see how this is
MyClass *nextOneOfMe; Here is the implementation file handled.
}
- (void) methodf;
CategoryName.m: Exception Handling
- (void) methodg: (ClassA *) argname; #import "CategoryName.h" As of MacOS X 10.3, exceptions are very similar to Java’s.
- (void) initWithData: (Data *) data @implementation ClassName ( CategoryName ) Here’s an example:
andParent: (MyClass *) parent; // method definitions @throw myException;
+ (void) classMethod; @end ...
@end Note: Categories that extend the class NSObject are called @try {
informal protocols, and behave much like a protocol: they [cup fill];
Implementation File specify a set of behaviors that a particular object has. See } @catch (NSException *exception) {
Misc Hints for testing whether an object has a behavior. NSLog(@"main: Caught %@: %@",
MyClass.m: [exception name], [exception reason]);
Note2 : Categories can be declared within an implementation
} @finally {
#import"MyClass.h" file, which is a common way to create “private” methods.
...
@implementation MyClass }
- (void) methodf NSString constants Misc Hints
{
// do something good NSString aString = @"the value of aString"; Here’s how you do the equivalent of Java’s instanceof:
}
aString is a fully valid instance of NSString, so you can send [anObject isKindOfClass:[NSPopUpButton class]]
- (void) methodg: (ClassA *) arg
{ it messages like this: Works for a class, and
// do something good with arg int length = [aString length]; [anObject conformsToProtocol: @protocol(MyProtocol)]
} NSString upper = [@"Some String" uppercaseString]; checks whether an object implements a protocol, and
- (void) initWithData: (Data *) data [anObject respondsToSelector:
andParent: (MyClass *) parent; @selector(methodWithArg:)]is pretty much self-
{ explanatory.
// do something good with data and parent
@synchronized(anObject){...} ensures that only one thread
}
runs the enclosed code, using anObject as the lock.10.3 fea-
@end
ture.
Copyright
c 2004 MeCodeGoodSomeday LLC
v0.7 2004
designed by Dylan McNamee

You might also like