User Tools

Site Tools


ios-labs-s14:basics-methods

Methods

“Method”, “message”, and “function” all (sort of) refer to the same thing in Objective-C.

A method is a section of code that we can execute (call) from elsewhere in our code.

Components to methods:

  1. Declaration
  2. Definition
  3. Invocation

Declaration

Declaration- the act of informing other objects about a function's name, return type, and parameters.

Declarations for public methods are included in the header files of Objective-C objects.

Declarations for private methods are included in the Definition and do not need additional code. These methods can only be called within a class.

Declarations take the form:

  • - (RETURNTYPE) METHODNAME:(ARGUMENTTYPE*)ARGUEMENTNAME METHODNAME2:(ARGUMENTTYPE*)ARGUEMENTNAME2;

A dash at the start indicates a instance method. Can only be called on an instantiated object. When creating a method, always use a dash unless you have some overriding reason.

A plus sign at the start indicates a class method. Can only be called on the class itself, commonly used as custom initialization method.

Definition

Definition- writing the code that executes/is contained within the method. Known as the method body.

Methods are defined in implementation files, or .m files. The form is the same as the declaration, except with curly braces instead of a semicolon which contain the method body.

  • - (RETURNTYPE) METHODNAME:(ARGUMENTTYPE*)ARGUEMENTNAME METHODNAME2:(ARGUMENTTYPE*)ARGUEMENTNAME2 {CODE}

Invocation

Invocation- the act of running the code contained within a method. A method is invoked on a target, where the target is the object that owns the method.

Consists of two parts

  1. Target- the target object or class that owns the method; where the method will be invoked
  2. Name- the name of the method to be invoked

Invoking a method takes the form:

[TARGET NAME];

and can be nested infinitely deep:

[[[TARGET NAME] NAME2] NAME3];

where NAME2 and NAME3 are methods called on the objects returned by methods NAME and NAME2, respectively.

ios-labs-s14/basics-methods.txt · Last modified: 2014/02/26 13:02 by mbarboi