Calling a obj-c method with a parameter

自作多情 提交于 2019-12-23 08:57:03

问题


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?


回答1:


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.




回答2:


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

myString = [myObject myFuncWithParam: value];




回答3:


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.



来源:https://stackoverflow.com/questions/607528/calling-a-obj-c-method-with-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!