Tuesday, 2 June 2015

Objects and Method parameters and Return types in Objective C

Objects: In general objects are the real world entity. Earlier the two functions communicate with each other using the function call(say programming in c) but the programmer need more better approach for communication between the two functions or classes so the concept of object is implemented. Using the concept of objects the message passing is became more easier and programmer can use it in more effective way to pass the message from one class to another or for accessing the members of the class.
Usage of objects:
1) To allocate the memory.
2) To access the member of the class(that include member Function and member functions).
3) Reusability Of code.
how to Create Objects:
  1. @interface Person : NSObject
  2. @property NSString *firstName;
  3. @property NSString *lastName;
  4. -(void)showEmplRecord();
  5. @end
here we have created a class call person and it have some attributes like firstname and lastname and they are of string dataType… for accessing them use the object of that class for example…
  1. classname *objectname=[[classname alloc]init];
  2. object of the class will be created as:
  3. Person *perobj=[[Person alloc]init];
  4. And we can access the class member as:
  5. Perobj.firstName;
  6. Perobj.lastName;
Methods: A method is a procedure or process to get some task done with some specific instructions. In other language like C, the method name is refer to a block of code in receiver class by compiler. In Objective C language the receiver of message is identified at run time with the help of object on which the message being passed. That means at compile time message passing system do not check if there is any receiver of message to receive it or not. It can happen that the method for the message is not implemented in receiver class. In this case it will raise an exception.
Class Methods are called on the class name itself, independent of an instance: [classname methodName]; in above example method can be called as [Person showEmplRecord];

Parameter of a method: 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 method. Arguments are optional that is, a method may contain no argument. If the method has no argument is the parameter list will be blanked. And if the parameter has some argument they are listed after the method name like this-
Method with no argument: - (void) max;
Method with 1 argument: : - (void) max:(int) num1 ;
Method with More Than 1 argument: - (void) max:(int) num1 secondNumber: (int) num2 ;

Return type of a method: The return type of a function defines the type of the result that method needs to return if it was declared with a return type. The resultant return type must match the return type of method declaration. To return a value from method to message passing object return keyword must be used. If a function is declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.  Simple code to under stand the concept of return:
  1. #import Foundation/Foundation.h>
  2. -(int)sum: (int )a secondPara:(int)b;
  3. int main (int argc, const char * argv[])
  4. {
  5. NSLog (@"Sum Of the No are=%d");
  6. return 0;
  7. }
  8. -(int)sum: (int )a secondPara:(int)b
  9. {
  10. return a+b;
  11. }
If a method is declared with any return type than it should return a value.. See In example the function sum declared with the int datatype so it is returning the int value(return a+b). and the main method is returning zero to the operating system that it is terminating normally.


For such more Blogs you can visit to http://findnerd.com/NerdDigest

No comments:

Post a Comment