Private methods appear as public methods

I am trying to improve the design of my application using private methods. Coming from .NET, I got a little confused because I declare these methods in a .m file, but from the other files that they are still displayed, that is, they are still available.

.m file:

@interface NSContentWebServiceController (private) - (NSString *)flattenHTML:(NSString *)html; - (NSString *)cleanseStringOfJsonP:(NSString *)jsonP; - (void)retrieve:(NSasdf *)hasdel :(NSDictionary *)rootList; - (NSString *)removeHTMLTagsFromString:(NSString *)aString; @end 
+4
source share
2 answers

Private methods are only private, so they are not documented in the header file. Because of this, you cannot #import include them in your project and, thus, the compiler will warn you that the "selector is not recognized" or something like that.

You will be able to call these methods in the same way as public methods, because this is just where you declare a prototype that makes the method private, Objective-C does not have such a thing as hidden, really private methods.

At run time, you can always find all methods using introspection, so it is really impossible to completely hide your methods / properties.

You can add an instance variable id _internal , which points to an object that does all the work, all the more difficult to call private methods, although this is not possible.

+3
source

As JoostK said, there are no private methods in Objective-C, as you have in C ++, Java, or C #.

In addition, the @interface NSContentWebServiceController (private) expression defines a so-called category in Objective-C. The term private is simply the name of a category and does not make sense. Having something like yellowBunny here will give the same effect. A category is just a way to break a class into several parts, but at run time all categories are valid. Note that a category can only add new methods to an object class, but not new variables.

For private categories, they now prefer to use an anonymous category, as in @interface MyClass() , since then you do not need a separate @implementation MyClass(yellowBunny) , but can simply add methods to the main @implementation block.

See the Categories section of the Objective-C Wikipedia entry for more information.

+5
source

All Articles