Methods
Class methods
Section titled “Class methods”A class method is called on the class the method belongs to, not an instance of it. This is possible because Objective-C classes are also objects. To denote a method as a class method, change the - to a +:
+ (void)hello { NSLog(@"Hello World");}Method parameters
Section titled “Method parameters”If you want to pass in values to a method when it is called, you use parameters:
- (int)addInt:(int)intOne toInt:(int)intTwo { return intOne + intTwo;}The colon (:) separates the parameter from the method name.
The parameter type goes in the parentheses (int).
The parameter name goes after the parameter type.
Create a basic method
Section titled “Create a basic method”This is how to create a basic method that logs ‘Hello World” to the console:
- (void)hello { NSLog(@"Hello World");}The - at the beginning denotes this method as an instance method.
The (void) denotes the return type. This method doesn’t return anything, so you enter void.
The ‘hello’ is the name of the method.
Everything in the {} is the code run when the method is called.
Return values
Section titled “Return values”When you want to return a value from a method, you put the type you want to return in the first set of parentheses.
- (NSString)returnHello { return @"Hello World";}The value you want to return goes after the return keyword;
Calling methods
Section titled “Calling methods”Calling an instance method:
[classInstance hello];
@interface Sample -(void)hello; // exposing the class Instance method @end
@implementation Sample -(void)hello{ NSLog(@"hello"); } @endCalling an instance method on the current instance:
[self hello];
@implementation Sample
-(void)otherMethod{ [self hello]; }
-(void)hello{ NSLog(@"hello"); } @endCalling a method that takes arguments:
[classInstance addInt:1 toInt:2];
@implementation Sample -(void)add:(NSInteger)add to:(NSInteger)to NSLog(@"sum = %d",(add+to)); } @endCalling a class method:
[Class hello];
@interface Sample +(void)hello; // exposing the class method @end
@implementation Sample +(void)hello{ NSLog(@"hello"); } @endInstance methods
Section titled “Instance methods”An instance method is a method that’s available on a particular instance of a class, after the instance has been instantiated:
MyClass *instance = [MyClass new];[instance someInstanceMethod];Here’s how you define one:
@interface MyClass : NSObject
- (void)someInstanceMethod; // "-" denotes an instance method
@end
@implementation MyClass
- (void)someInstanceMethod { NSLog(@"Whose idea was it to have a method called \"someInstanceMethod\"?");}
@endPass by value parameter passing
Section titled “Pass by value parameter passing”In pass by value of parameter passing to a method, actual parameter value is copied to formal parameter value. So actual parameter value will not change after returning from called function.
@interface SwapClass : NSObject
-(void) swap:(NSInteger)num1 andNum2:(NSInteger)num2;
@end
@implementation SwapClass
-(void) num:(NSInteger)num1 andNum2:(NSInteger)num2{ int temp; temp = num1; num1 = num2; num2 = temp;}@endCalling the methods:
NSInteger a = 10, b =20;SwapClass *swap = [[SwapClass alloc]init];NSLog(@"Before calling swap: a=%d,b=%d",a,b);[swap num:a andNum2:b];NSLog(@"After calling swap: a=%d,b=%d",a,b);Output:
2016-07-30 23:55:41.870 Test[5214:81162] Before calling swap: a=10,b=202016-07-30 23:55:41.871 Test[5214:81162] After calling swap: a=10,b=20Pass by reference parameter passing
Section titled “Pass by reference parameter passing”In pass by reference of parameter passing to a method, address of actual parameter is passed to formal parameter. So actual parameter value will be changed after returning from called function.
@interface SwapClass : NSObject
-(void) swap:(int)num1 andNum2:(int)num2;
@end
@implementation SwapClass
-(void) num:(int*)num1 andNum2:(int*)num2{ int temp; temp = *num1; *num1 = *num2; *num2 = temp;}@endCalling the methods:
int a = 10, b =20;SwapClass *swap = [[SwapClass alloc]init];NSLog(@"Before calling swap: a=%d,b=%d",a,b);[swap num:&a andNum2:&b];NSLog(@"After calling swap: a=%d,b=%d",a,b);Output:
2016-07-31 00:01:47.067 Test[5260:83491] Before calling swap: a=10,b=202016-07-31 00:01:47.070 Test[5260:83491] After calling swap: a=20,b=10