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

Next revision
Previous revision
ios-labs-s14:basics-methods [2014/02/13 20:52]
mbarboi created
ios-labs-s14:basics-methods [2014/02/26 13:02]
mbarboi
Line 1: Line 1:
-The terms method”, “messages”, and “function” all kind of refer to the same thing in Objective-C. You may hear them used interchangeably,​ though there are differences. I will stick with the terminology “calling a method” in this post. A method (or message or function) is a section of code that we can call from elsewhere in our code, and the method will perform some action or return some kind of result that we can use. Methods are used to organize our code into reusable (and understandable) chunks that save us a lot of time and energy.+=====Methods==== 
 +Method”, “message”, and “function” all (sort ofrefer to the same thing in Objective-C.
  
-function declaration tells the compiler about function'​s name, return type, and parameters. A function definition provides the actual body of the function.+method is section ​of code that we can execute (call) from elsewhere in our code.
  
-A method definition in Objective-C programming language consists of a method header and a method body. Here are all the parts of a method:+Components to methods: 
 +  -Declaration 
 +  -Definition 
 +  -Invocation
  
-Defining a Method +====Declaration==== 
-Return Type: A 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 casethe return_type is the keyword void.+**Declaration**- ​the act of informing other objects about a function's namereturn type, and parameters
  
-Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.+Declarations for public methods are included in the header files of Objective-C objects
  
-Arguments: A argument is like a placeholder. When 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 methodArguments are optional; that is, method may contain no argument.+Declarations for private methods are included in the Definition ​and do not need additional codeThese methods can only be called within ​class.
  
-Joining ArgumentA joining argument is to make it easier to read and to make it clear while calling it.+Declarations take the form: 
 +  *- (RETURNTYPE) METHODNAME:​(ARGUMENTTYPE*)ARGUEMENTNAME METHODNAME2:​(ARGUMENTTYPE*)ARGUEMENTNAME2;​
  
-Method Body: The method ​body contains ​collection of statements that define what the method ​does.+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.
  
-Declaration +A plus sign at the start indicates a **class method**. Can only be called on the class itself, commonly used as custom initialization method. 
-Method declaration ​is required when you define a method in one source file and you call that method ​in another fileIn such case you should declare ​the function at the top of the file calling ​the function.+ 
 +====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 
 +  -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 
 + 
 +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