6

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)?

2
  • Perhaps you should put xcode and objective-c as tags for your question? Commented Dec 27, 2009 at 20:42
  • If you find an answer useful, mark it as so by clicking the checkmark.
    – GManNickG
    Commented Dec 27, 2009 at 20:47

4 Answers 4

24

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.

1
  • Just (void) is valid in objective c?
    – mrgloom
    Commented Apr 2, 2019 at 14:55
8

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.

2

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

0
0

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.