xcode 这个函数声明中的“-(void)”是什么意思?`-(void)awakeFromNib`

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1966959/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:01:36  来源:igfitidea点击:

What does "-(void)" mean in this function declaration? `-(void)awakeFromNib`

objective-cxcode

提问by nambvarun

How come whenever I have to use awakeFromNib protocol I have to put it in this format?

为什么每当我必须使用awakeFromNib 协议时,我都必须将它放在这种格式中?

-(void)awakeFromNib

What is the need for -(void)?

-(void) 需要什么?

回答by UncleO

The -(void)is used in the declaration of the method. Presumably, you are defining it for someone else to call, rather than calling it yourself.

-(void)是在该方法的声明中使用。据推测,您正在定义它以供其他人调用,而不是自己调用。

The -sign indicates that the method is an instance method, as opposed to a class method. It requires an object to call it, and instance variables of the object are available to it inside its definition.

-符号表示,该方法是一个实例方法,相对于一个类的方法。它需要一个对象来调用它,并且该对象的实例变量在其定义中可供它使用。

The (void)indicates the return type. This method doesn't return anything, so its result can't be assigned to anything.

(void)指示返回类型。这个方法不返回任何东西,所以它的结果不能分配给任何东西。

回答by Dean

think of it this way

这样想

say you have a Class you created that is called "Math"

假设您创建了一个名为“Math”的类

and this class has a method called "calculate". It's type as

这个类有一个叫做“calculate”的方法。它的类型为

-(int)calculate {
2+2;
return 2+2;
}

When you alloc the class and initialize the object and perform the "calculate method on that object, it's going to do the calculation 2+2 and it will return the result, 4.

当您分配类并初始化对象并对该对象执行“计算方法”时,它将执行计算 2+2 并返回结果 4。

If you tried

如果你试过

-(void)calculate {
2+2;
}

it wouldn't do anything, it would just have that 2+2 information stored in the method but the calculation would never occur.

它不会做任何事情,它只会在方法中存储 2+2 信息,但永远不会发生计算。

回答by Chuck

Because the method does not return anything, and giving a void return type is how you declare that in C and Objective-C.

因为该方法不返回任何内容,并且给出 void 返回类型是您在 C 和 Objective-C 中声明的方式。

回答by Steven Schlansker

(void) marks the return type - in this case, void means it's returning nothing.

(void) 标记返回类型 - 在这种情况下,void 表示它不返回任何内容。

If it was instead -(int)awakeFromNib, you'd be expected to return an integer. The meaning of the return value (if any) should be explained in the documentation.

如果它是 -(int)awakeFromNib,你应该返回一个整数。应该在文档中解释返回值(如果有)的含义。