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)?
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.
Because the method does not return anything, and giving a void return type is how you declare that in C and 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.