Implementing the drawHypnoticMessage Method


#1

I have a misunderstanding I’m hoping someone can help me out with. I added the method and everything runs fine. What confuses me, is that I was under the impression if the class implemented a method, the method definition should be in the .h file, and them it’s implemented in the .m file. In this case we implemented a new method, but never added a line in the .h file for it. What’s the reason for this?


#2

This is usually done in the implementation files to hide details, those details that should not be in the interface file. (Doing this simplifies the interface.)

You can implement a method without declaring it in the .h file. Your code will still work, provided you invoke the method correctly from outside the implementation file; however, you may get warnings from Xcode. This is Objective-C, the most dynamic language in the entire solar system!


#3

The only reason you’d declare a method in the header file is if another class needs to use that method. If the method is just used within a class, then there is no reason to declare it in the header file. Additionally, you probably don’t want to declare it in the header file, as then other classes would then know about that method that you intended to be private.


#4

But then, if you didn’t look in the .m file you wouldn’t know that the method exists?
Isn’t that one of the reasons to have the .h file which serves as an interface to the class? Wouldn’t it be like looking inside your t.v for the volume knob because it isn’t on the remote control?
[color=#FF0000]OR.[/color]. is it because the method is only used within the delegate method and no where else, so then you wouldn’t need to use it standalone… I assume this is the case anyway as its the most logical.


#5

[quote=“DannyBoy”]But then, if you didn’t look in the .m file you wouldn’t know that the method exists?
Isn’t that one of the reasons to have the .h file which serves as an interface to the class? Wouldn’t it be like looking inside your t.v for the volume knob because it isn’t on the remote control?
[color=#FF0000]OR.[/color]. is it because the method is only used within the delegate method and no where else, so then you wouldn’t need to use it standalone… I assume this is the case anyway as its the most logical.[/quote]

You don’t want it in the header file if it’s a private method, as you are then exposing it to the world. If you really want to declare all of your methods, then you can add the “private method” declarations to the class extension in the .m file. (We used to need to do this, but today this practice has been dying since Xcode 4.4 make it not necessary.)

@interface BNRHypnosisViewController () <UITextFieldDelegate>

- (void)drawHypnoticMessage;

@end

Also, the problem with your example is that changing the volume SHOULD be public, so you’d want it in the header file. A better analogy would be “turning the 5th pixel in the 4th column blue,” something a user of a TV would (and should) never need to do.


#6

Great thanks!
So, would you say that my second example is more to the point then…?


#7

[quote=“DannyBoy”]Great thanks!
So, would you say that my second example is more to the point then…?[/quote]
Not necessarily. My point is that it is because it is only used in the .m file, and not used outside of the class. Personally, if I used that method in multiple places in the .m file, but only in the .m file, I still wouldn’t add it to the header file.

The only time you want to add a method to the header file is if other classes need to use that method.


#8

From what I’ve learned so far it seems that protocol contains only the names of the methods whether optional or not and therefore the implementation is left for the user in the .m file. What is more you could use the protocol methods only if the class conforms to it. In the same time you don’t have to write them in the header file as they exist inside of the protocol.