How to call a function in Objective C? For example:
I define the function in header (.h file):
-(void)abc
and implement this function in implementation file (.m file):
-(void)abc
{
//.....
///....
}
Now how would I call this function from where I need it?
It represent id Object. that you can use any expression in it or return any object. Syntax : @(<#expression#>) it will return id object.
You really can't use C in Objective-C, since Objective-C is C. The term is usually applied when you write code that uses C structures and calls C functions directly, instead of using Objective-C objects and messages.
Objective-C uses the same phraseology as the C language. Like in C, each line of Objective-C code must end with a semicolon. Blocks of code are written within a set of curly brackets. All basic types are the same, such as int, float, double, long and more.
To call this method from within the same class you would call :
[self abc];
To call from another class, assuming you have a reference to an instance of that class you would call :
[instance abc];
If you have parameters in the method, for the first parameter you would declare it as (assuming it is a string) :
- (void) abc : (NSString *)param1;
And call it as :
[self abc:@"Yoop"];
All following parameters must be given a name. So for example if there were two parameters you would declare it as :
- (void) abc : (NSString *)param1 paramName2:(NSString *)param2;
This would be called like :
[self abc:@"Yoop" paramName2:@"Woop"];
It does take a little getting used to to start with, but there is method to the madness! In Objective-C terminology you arent really calling the method, you are passing a message. This is a good blog post describing the differences : Cocoa with Love
I discuss this here: What's with the square brackets (calling methods)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With