Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call an Objective C function?

Tags:

objective-c

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?

like image 406
user313396 Avatar asked Apr 13 '10 09:04

user313396


People also ask

What does @() mean in Objective-C?

It represent id Object. that you can use any expression in it or return any object. Syntax : @(<#expression#>) it will return id object.

Can I use C in Objective-C?

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.

How do you write Objective-C?

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.


1 Answers

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)

like image 63
Mongus Pong Avatar answered Sep 22 '22 15:09

Mongus Pong