Alan
Alan

Reputation: 1275

Calling a obj-c method with a parameter

I've change a c-style function to an objective-c method. As a method, how do i use it?

    NSString* myfunc( int x )

       is now:

    - (NSString *)myFuncWithParam:(int)x


 c code:  myString = myfunc(x);  // works

 obj-c code: myString = myFuncWithParam(x); // fails to compile. 

From one of the answers: myString = [object myFuncWithParam:x];

In that case, what would "object" be?

Upvotes: 9

Views: 23105

Answers (3)

Marc Charbonneau
Marc Charbonneau

Reputation: 40513

myString = [object myFuncWithParam:x];

Where object is the object which has the method you're calling. If you're calling it from the same object, you'll use 'self'. This tutorial might help you out in learning Obj-C.

Upvotes: 17

James Curran
James Curran

Reputation: 103605

Off topic/old man's ramblings:

Once, when I was bored, I tried to create a Obj-C-like syntax for C++ using operator overloading. I believe I was able to get

myString = myObject[myFuncWithParam](value); 

to work.

Upvotes: 0

Mike Marshall
Mike Marshall

Reputation: 7850

You need to use the square bracket "message" syntax:

myString = [myObject myFuncWithParam: value];

Upvotes: 0

Related Questions