Objective-C is a superset of CLanguage, adding an object system very similar to that of SmalltalkLanguage. The extension is incredibly minuscule, compared to, e.g., CPlusPlusLanguage.
Most Objective-C programmers are Mac programmers who use Cocoa, an Objective-C framework based on the OpenStep specification, including many common data structures (NSArray, NSString, NSDictionary, et cetera) in the Foundation part, and an extensive GUI toolkit in the AppKit part.
The Objective-C newsgroup is comp.lang.objective-c. Be forewarned that there is one rabid, inflammatory lunatic named David Stes who has an irrational hatred of everything about Apple, NeXT, and OpenStep; for this reason there are innumerable flamewars very often -- avoid threads involving lots of posts by David Stes, unless they're about his Objective-C to C compiler called 'POC' (the Portable Object Compiler).
Objective-C extends C with the following things:
id type -- the type of all objects
BOOL type and constants YES and NO
SEL type -- the type of selectors
syntax for making selectors: @selector(foo:bar:baz:)
IMP type -- the type for method implementation pointers
Class type -- the type of all classes.
nil -- the null object
Nil -- the null class
syntax for message passing: [object foo: arg1 bar: arg2] (like the equivalent in SmalltalkLanguage: object foo: arg1 bar: arg2)
- syntax for declaring classes:
@interface Classname : Superclass < Protocols >
// The protocols list (including the angle brackets) may be omitted.
// The ': Superclass' may also be omitted (of course, the protocols list may be present
// without the superclass), in which case the class declared is a root class -- it has no
// superclass.
{
id ivar1;
int ivar2;
}
+ (ret_type)classMethod: (Foo *)arg1 bar: (Bar *)arg2;
- (ret_type)instanceMethod: (Baz *)arg1 quux: (Quux *)arg2;
@end- syntax for defining classes:
@implementation Classname
+ (ret_type)classMethod: (Foo *)arg1 bar: (Bar *)arg2
{
[something do: arg1 stuff: arg2 with: [somethingElse foo: arg1 bar: arg2]];
}
- (ret_type)instanceMethod: (Baz *)arg1 quux: (Quux *)arg2
{
// Within method implementations, three special variables are defined:
// self - pointer to the current object (or class, in the case of class methods
// super - pointer to the current object, but which begins searching the superclass
// for methods, instead of the current class
// _cmd - the selector of the current method
DoSomething(arg1, [self quux: arg2]);
}
@end- protocols (what Java calls 'interfaces' are really protocols, and Objective-C had them first):
@protocol Protocolname < Other Protocols > // If the protocols list is not omitted, then 'Protocolname' includes all the protocols in the // list as well as the methods it defines itself. + (ret_type)classM: (struct mumble) mumble; - instM: (union zoink *) zoink; // if no return type is specified, 'id' is default. @end
- categories (adding methods to existing classes):
// Category interface
@interface Classname (Categoryname)
+ foo: (Foo *)foo;
@end
// Category implementation
@implementation Classname (Categoryname)
+ foo: (Foo *)foo
{
[foo doSomethingWith: self]; // 'self' refers to the current class in class methods
}
@endsyntax for constant string objects: @"foo" (uses a special compiler-specific class -- there are no built-in classes in Objective-C besides Nil, which isn't really a class anyways, just (Class)0)
private/protected/public instance variables: (but no one uses them anyways. The default is @protected)
@interface Foo : Bar
{
@public
// Anyone can do: object->foo or object->bar
id foo;
float bar;
@protected
// Only methods of this class and subclasses can do self->baz
int baz;
@private
// Only methods of this class can do self->quux
double quux;
}very little more (like @defs and @encode)
Obligatory 'Hello, World!' example, using OpenStep:
main.m: (the Objective-C file extension is '.m' Don't ask me where the 'm' came from.)
#import <Foundation/Foundation.h>
int main (int argc, char **argv, char **envp)
{
// Has to do with OpenStep's memory management.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFileHandle *out = [NSFileHandle fileHandleWithStandardOutput];
// For some reason, there's no writeString: method or anything.
[out writeData: [NSData dataWithBytes: "Hello, World!\n" length: 14];
[pool release];
return 0;
}