Mac OS X Reference Library Apple Developer
Search

Objective-C Quick Reference Guide

The Objective-C language is a superset of ANSI C with special syntax and run-time extensions that make object-oriented programming possible. Objective-C syntax is uncomplicated but powerful in its simplicity. You can mix standard C with Objective-C code.

The following sections summarize some of the basic aspects of the language. See The Objective-C Programming Language for details.

Messages and Method Implementations

Methods are procedures implemented by a class for its objects (or, in the case of class methods, to provide functionality not tied to a particular instance). Methods can be public or private; public methods are declared in the class header file. Messages are invocations of an object’s method that identify the method by name.

Message expressions consist of a variable identifying the receiving object followed by the name of the method you want to invoke; enclose the expression in brackets.

[anObject doSomethingWithArg:this];

As in standard C, terminate statements with a semicolon.

Messages often result in values being returned from the invoked method; you must have a variable of the proper type to receive this value on the left side of an assignment.

int result = [anObj calcTotal];

You can nest message expressions inside other message expressions. This example gets the window of a form object and makes the returned NSWindow object the receiver of another message.

[[form window] makeKeyAndOrderFront:self];

A method is structured like a function. After the full declaration of the method comes the body of the implementing code enclosed by braces.

Use nil to specify a null object; nil is analogous to a null pointer. Note that some Cocoa methods do not accept nil as an argument.

A method can usefully refer to two implicit identifiers: self and super. Both identify the object receiving a message, but they differ in how the method implementation is located: self starts the search in the receiver’s class whereas super starts the search in the receiver’s superclass. Thus,

[super init];

causes the init method of the superclass to be invoked.

In methods you can directly access the instance variables of your class’s instances. However, accessor methods are recommended instead of direct access, except in cases where performance is paramount.

Declarations

Dynamically type objects by declaring them as id.

id myObject;

Since the class of dynamically typed objects is resolved at runtime, you can refer to them in your code without knowing beforehand what class they belong to. Type outlets and objects in this way if they are likely to be involved in polymorphism and dynamic binding.

Statically type objects as a pointer to a class.

NSString* mystring;

You statically type objects to obtain better compile-time type checking and to make code easier to understand.

Declarations of instance methods begin with a minus sign (-); a space after the minus sign is optional.

- (NSString*)countryName;

Put the type of value returned by a method in parentheses between the minus sign (or plus sign for class methods) and the beginning of the method name. Methods that return nothing must have a return type of void.

Method argument types are in parentheses and go between the argument’s keyword and the argument itself.

- (id)initWithName:(NSString*)name andType:(int)type;

Be sure to terminate all declarations with a semicolon.

By default, the scope of an instance variable is protected, making that variable directly accessible only to objects of the class that declares it or of a subclass of that class. To make an instance variable private (accessible only within the declaring class), insert the @private compiler directive before the declaration.




Last updated: 2009-08-03

Did this document help you? Yes It's good, but... Not helpful...