如何在 Objective-C 中调用方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/591969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I call a method in Objective-C?
提问by nduplessis
I am trying to build an iPhone app. I created a
method like this:
我正在尝试构建一个 iPhone 应用程序。我创建了一个
这样的方法:
- (void)score {
// some code
}
and I have tried to call it in an other method like this:
我试图用这样的其他方法调用它:
- (void)score2 {
@selector(score);
}
But it does not work. So, how do I call a method correctly?
但它不起作用。那么,如何正确调用方法呢?
回答by nduplessis
To send an objective-c message in this instance you would do
要在这种情况下发送一条 Objective-c 消息,您可以这样做
[self score];
I suggest you read the Objective-C programming guide Objective-C Programming Guide
我建议你阅读 Objective-C 编程指南 Objective-C 编程指南
回答by Chuck
I suggest you read The Objective-C Programming Language. The part about messaging is specifically what you want here, but the whole thing will help you get started. After that, maybe try doing a few tutorials to get a feel for it before you jump into making your own apps.
我建议你阅读The Objective-C Programming Language。关于消息传递的部分正是您在这里想要的,但整个过程将帮助您入门。在那之后,在开始制作自己的应用程序之前,也许可以尝试做一些教程来了解它。
回答by Ferruccio
I think what you're trying to do is:
我认为你想要做的是:
-(void) score2 {
[self score];
}
The [object message]syntax is the normal way to call a method in objective-c. I think the @selectorsyntax is used when the method to be called needs to be determined at run-time, but I don't know objective-cwell enough to give you more information on that.
该[object message]语法是调用在目标C的方法以正常的方式。我认为@selector当需要在运行时确定要调用的方法时会使用该语法,但我不太了解objective-c,无法为您提供更多信息。
回答by Mohammed Rashwan
calling the method is like this
调用方法是这样的
[className methodName]
however if you want to call the method in the same class you can use self
但是,如果您想在同一个类中调用该方法,则可以使用 self
[self methodName]
all the above is because your method was not taking any parameters
以上都是因为你的方法没有接受任何参数
however if your method takes parameters you will need to do it like this
但是,如果您的方法需要参数,您将需要这样做
[self methodName:Parameter]
回答by ganesh manoj
Use this:
用这个:
[self score]; you don't need @sel for calling directly
回答by PICKme
[self score]; instead of @selector(score)
回答by Sohaib Aslam
syntax is of objective c is
语法是目标 c 是
returnObj = [object functionName: parameters];
returnObj = [对象函数名:参数];
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.
其中 object 是具有您正在调用的方法的对象。如果你从同一个对象调用它,你将使用'self'。本教程可能会帮助您学习 Obj-C。
In your case it is simply
在你的情况下,它只是
[self score];
【自我评分】;
If you want to pass a parameter then it is like that
如果你想传递一个参数,那就是这样
- (void)score(int x) {
// some code
}
and I have tried to call it in an other method like this:
我试图用这样的其他方法调用它:
- (void)score2 {
[self score:x];
}
回答by manishkumar
use this,
[self score];
instead of @selector(score).

