9

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?

3
  • You should probably edit your question's title to be "Calling an objective-C method with a parameter". Commented Mar 3, 2009 at 18:25
  • I've updated my answer to describe what object is... You may want to study up a bit on object oriented programming before continuing with Obj-C since it's a very important concept. Commented Mar 3, 2009 at 19:05
  • You can't just slightly change the syntax and expect procedural C code to suddenly work as Objective-C. That's like appending "o" to all your words and imagining that you're speaking Spanish. You need to actually learn Objective-C. Commented Mar 3, 2009 at 19:14

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

myString = [myObject myFuncWithParam: value];

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.