简体   繁体   中英

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

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

-(void)awakeFromNib

What is the need for -(void)?

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.

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.

think of it this way

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

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

-(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.

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM