user313396
user313396

Reputation: 239

How do I call an Objective C function?

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?

Upvotes: 15

Views: 24316

Answers (3)

Sikander
Sikander

Reputation: 483

it's too easy just write

[self function];

Upvotes: 0

Mongus Pong
Mongus Pong

Reputation: 11477

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)

Upvotes: 26

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

This is a method on some class. If the class is called Foo, it might be something like this:

Foo* foo = [[Foo alloc] init];
[foo abc];

Upvotes: 2

Related Questions