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:

@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

@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

@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

// 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
}
@end

@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;
}

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;
}

ObjCLanguage (last edited 2008-07-09 05:47:50 by localhost)