Tutorial by Examples

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 aft...
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. ...
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;
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"); }
Calling an instance method: [classInstance hello]; @interface Sample -(void)hello; // exposing the class Instance method @end @implementation Sample -(void)hello{ NSLog(@"hello"); } @end Calling an instance method on the current instance: [self hell...
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; // "-&qu...
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 @impleme...
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 S...

Page 1 of 1