User Tools

Site Tools


ios-labs-s14:basics-methods

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ios-labs-s14:basics-methods [2014/02/17 11:20]
mbarboi
ios-labs-s14:basics-methods [2014/02/26 13:02]
mbarboi
Line 2: Line 2:
 “Method”,​ “message”,​ and “function” all (sort of) refer to the same thing in Objective-C. “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 call from elsewhere in our code.+A method is a section of code that we can execute (callfrom elsewhere in our code.
  
 Components to methods: Components to methods:
Line 18: Line 18:
 Declarations take the form: Declarations take the form:
   *- (RETURNTYPE) METHODNAME:​(ARGUMENTTYPE*)ARGUEMENTNAME METHODNAME2:​(ARGUMENTTYPE*)ARGUEMENTNAME2;​   *- (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====
 **Definition**- writing the code that executes/is contained within the method. Known as the method body. **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. 
  
-Defining a Method +  *- (RETURNTYPE) METHODNAME:(ARGUMENTTYPE*)ARGUEMENTNAME METHODNAME2:(ARGUMENTTYPE*)ARGUEMENTNAME2 {CODE}
-Return TypeA method may return a value. The return_type is the data type of the value the function returns. Some methods perform the desired operations without returning a value. In this case, the return_type is the keyword void. +
- +
-Method NameThis is the actual name of the method. The method name and the parameter list together constitute the method signature.+
  
-Arguments: A argument is like placeholderWhen a function ​is invoked, ​you pass a value to the argument. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the arguments of a method. Arguments are optional; that is, a method ​may contain no argument.+====Invocation=== 
 +**Invocation**- the act of running the code contained within ​methodA method ​is invoked ​on a targetwhere the target ​is the object that owns the method.
  
-Joining Argument: A joining argument is to make it easier to read and to make it clear while calling it.+Consists of two parts 
 +  -Target- the target object or class that owns the method; where the method will be invoked 
 +  -Name- the name of the method ​to be invoked
  
-Method Body: The method body contains ​collection of statements that define what the method ​does.+Invoking ​a method ​takes the form: 
 +  [TARGET NAME]; 
 +  
 +and can be nested infinitely deep: 
 +  [[[TARGET NAME] NAME2] NAME3];
  
-Declaration +where NAME2 and NAME3 are methods called on the objects returned by methods NAME and NAME2, respectively.
-Method declaration is required when you define a method in one source file and you call that method in another file. In such case you should declare ​the function at the top of the file calling the function.+
ios-labs-s14/basics-methods.txt · Last modified: 2014/02/26 13:02 by mbarboi