So I’ve recently been using more private interfaces in my classes. I’ve been leaving the stuff that is needed by other classes in the .h (public), and then keeping all other methods in the private interface in the .m. My question is, I don’t see any reason to keep any methods in my private interface as the app works fine without this. What is really the point? Any why aren’t system methods like viewDidLoad in the private interface?
So I’ve recently been using more private interfaces in my classes. I’ve been leaving
Share
That’s perfect. In some cases, you will want to use naming conventions or prefixes to avoid naming collisions. Moving on…
Encapsulation. Hide your data and implementation from your clients (and even subclasses) in order to minimize the side effects of any changes you must make. If the client really only needs 3 methods in the public interface — declare only those 3 methods. Keep the remainder of the implementation details private to the class. When you don’t use this, mutant implementations tend to multiply where clients use these unnecessary methods. Which means that when you need to change something, you will have a ton of detail to review across several source files (and apps, if you write libraries). A class’ public interface is a good area to focus on being minimal.
viewDidLoadwas designed to be overridden by subclasses, where necessary. Its declaration is public for 2 primary reasons:[super viewDidLoad], and the compiler can be assured you are doing the right thing. IfUIViewControllerhad not declared it as an instance method, there would be at least one compiler warning.