After you’ve got to grips with the basics of Objective-C, you’ll want to discover (or recover) features available in most object idioms.
Objective-C has its drawbacks; it also provides a few useful facilities that Java, Python and ActionScript developers might be crying for.
Classes
Like in C++, classes are usually specified and implemented using separate files. An @interface (not to be confused with a java interface) marks the beginning of a class specification. An @implementation marks the beginning of an… implementation. XCode provides ready templates for classes, so I won’t load this article with sample code.
Interfaces
In Objective-C, An ‘interface’, like a java or as2/3 interface, is called a protocol. Here is how you define a protocol (below <NSObject> refers to the NSObject protocol, not the NSObject class; minimally you surely want the compiler to know that a Foo variable is an object; see ‘inheritance’):
// Foo.h @protocol Foo <NSObject> // methods ... @end
Here’s how you declare that a class implements one or several protocols:
@interface Widget : NSObject <Foo, Bar, ... >
Finally, here’s (not a good surprise) how you declare a variable compliant with a given protocol:
id<Foo> foo;
Enums
Enums in Objective C are practically a legacy feature from C – for example:
typedef enum{
GD_ACTOR, GD_PROP, GD_TERRAIN, GD_TOKEN
} GDSequenceElementType;
To be accurate, this typedefs an anonymous enum. Don’t be fooled by the terminology – the end result is that you can refer the enum type without prefixing with ‘enum’ later on, which is what we want to do most of the time (there’s a lawyer explanation on StackOverflow… …somewhere)
Inheritance
Like Java and C++, Objective – C lets you define a superclass for a class, making it a derived class. You declare the superclass in the class interface (.h file), not in the class implementation.
@interface MyClass: ASuperClass
You’re probably familiar with this notation already as all objective C objects in CocoaTouch inherit from NSObject or a subclass thereof. You can also declare super-protocols for a protocol, like this:
@protocol Foo <Bar> // methods ... @end
Privacy
in Objective C, a special use of so-called categories lets you hide functions from other classes:
// Foo.o (not Foo.h!) @interface Foo (private) // private methods @end @class Foo // implement everything Foo, including private methods @end
Put simply you declare additional Foo methods as Foo implementation details. Since these declarations are within Foo.o and users of Foo import Foo.h, your methods are hidden. Dynamic binding would likely allow accessing these methods anyway, but that’s still hmm… pretty good privacy?
Class (static) members
Like most object idioms, Objective-C allows defining static fields and methods. The + modifier is used to denote a static method while the static modifier is used to denote a static field.
Important: You can’t really define constants using public, static variables in Objective C (simply put, you can’t make your static variables public). Although static vars have uses, I feel that many programmers end up defining constants using macros.
static int aStaticVar; +(int) aStaticFunction;
Constants
Objective C supports both class and external constants, widely used (again, along with macros) to avoid repeating string literals and magic numbers (all numbers are magic!). If you want to share constants across several files, you can do this using extern constants. Otherwise, use class constants.
// in class implementation (.m file) static NSString* const NAME=@"john"; // in .h file to include/import in files using the constant. extern NSString* const NAME; // in .m file associated with .h file. extern NSString* const NAME=@"john";
Macros and the Preprocessor
If you’re discovering C through Objective C, you might want to use the preprocessor for a variety of reasons. The most infamous use of the preprocessor is letting you designate arbitrary fragments of code as ‘macros’ that you can use throughout your code. Macros have many caveats and many programmers look down on them, which might explain we can’t use them in Java. I still think concision without the conceptual and design overheads associated with inheritance can be helpful and clarify code at times – never mind saving typing. You can learn about macros and the preprocessor from the following sources:
- The GNU C Preprocessor manual (this is reproduced verbatim in Apple docs)
- Tutorial on C Preprocessor from CProgramming.Com
Notes
(1) Objective-C provides another technique, called ‘Posing’, allowing to invoke super in function implementations. Posing is more complex and more powerful. Possibly unfortunately, this isn’t available in MacOS Leopard, and by extension not usable for iSomething development.


Comments