Private interface vs. private method - objective c
Solution 1
@interface Foo()
creates a class extension (I stand corrected, props to bbum) on interface Foo which is like additional methods added to the interface. Some people also use @interafce Foo(Private)
(category) instead of a class extension with ()
. It's more like "injecting" new methods into a class from outside the class.
Placing this in the .m file just keeps other things from "seeing it" in the .h file, but that's it. Basically people normally use categories or class extensions in .m files to specify private interfaces, but they are also used for things like UIKit uses categories to add row
and section
public methods to NSIndexPath. (This can be confusing.)
You don't really need to define private methods this way, but if you have a method called bar that calls method foo before foo is defined in the source file you'll get a compiler warning something like "object self may not respond to foo". You can get rid of that by defining foo before you define bar or any other foo-calling code. It's the same with plain C and functions.
Like Ole says this doesn't stop anyone from calling the private methods, it just declares your intention that they be private and causes the compiler to generate the "may not respond to" warnings even if they import the .h file.
EDIT
Also see http://www.friday.com/bbum/2009/09/11/class-extensions-explained/ for some explanation of categories vs. class extensions. Looks like class extensions should be more correct for defining private methods, from a compiler warning perspective, because category methods are optional. Wish my book would have explained this!
Solution 2
Objective-C has no totally private methods. The method declared in a private interface section in the .m file is invisible to outside callers but it is not private. If someone knows the method signature and ignores the compiler warning, they can call it from outside without problems.
Comments
-
joshim5 about 2 years
What is the difference between a private method and a private interface? For example, I know that if you define a method in an implementation and its interface does not mention it, it is considered a private method. I have also seen things such as:
@interface Collector() @property (readonly) NSMutableDictionary *count; @end
Inside of the .m implementation file.
-
bbum over 13 yearsThere is a huge difference between named categories
(foo)
and a class extension()
. -
Nimrod over 13 yearsDo you have a reference to this? I don't see anything in the book "Programming in Objective-C 2.0" about this. It seems like the category name is just optional from everything I can tell. Not that I'm a major expert or anything but it would be helpful if you could provide a link explaining the difference between (Private) and ().
-
Nimrod over 13 yearsnevermind, I seem to have found your blog friday.com/bbum/2009/09/11/class-extensions-explained Wonder why my book doesn't talk about this?
-
bbum over 13 yearsI'd forgotten I'd written that. Good thing google indexes my head.
-
Nimrod over 13 yearsWere class extensions some sort of "objective-c 2.1" thing? I thought I had the authoritative book on this subject but either I don't or something was added later....
-
johnw188 over 13 yearsI believe class extensions were introduced with objective C 2.0, though I might be mistaken.
-
Nimrod over 13 yearsThe only official-looking documentation I can find that mentions Extensions is the apple doc: developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/… I can't seem to find an official standards doc for Objective-C 2.0....
-
joshim5 over 13 yearsThanks guys! I also read Stephen Kochans book. I don't recall him talking about categories. I'm also watching stanfords CS193P. I'll check out bbum's blog as well as research categories.