Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private classes in Objective C

I would like a pattern for a nested private class in Objective C.

Requirements are:

  • class will not be visible/accessible to other classes.
  • class can execute methods (i.e., not a C struct)
  • containing class members are visible/accessible to the nested class

Considering the comments, I am simplifying the requirements:

  • inner class may be accessible to other classes, but not visible (similar to using a category to hide private methods).
  • inner class does not have to be nested

Is it still not possible?

like image 702
Ovesh Avatar asked Oct 07 '11 03:10

Ovesh


People also ask

How do you make a private method in Objective C?

To avoid exposing a method as part of a class's API, we do not declare it within the @interface section but within the @implementation section. Now, a private method like makeActivationSound can no longer be called by another class. A method call from a Jedi class like… [[Lightsaber new] makeActivationSound];

How do you make a method public in Objective C?

A property is saved in an object, so you can create an object ITYourCustomClass *objectOfYourCustomClass = [[ITYourCustomClass alloc] init]; and setting the properties objectOfYourCustomClass. title = @"something" . Then you can call [objectOfYourCustomClass doSomethingWithTheTitle]; , which is a public object method.


2 Answers

Objective-C has no notion of private classes or private instance variables in a formal declarative fashion.

Instead, visibility in Objective-C is entirely controlled by where you declare something. If it is in a header file, it can be imported by something else. If it is declared in an implementation file, it cannot (reasonably) be imported and, therefore, is effectively private to that compilation unit.

And by "it", I mean pretty much anything that can be declared; class, global, etc...

I.e. if you stick an @interface/@implementation pair for a class in a .m file, that class is effectively private to that compilation unit. Of course, without namespaces, make sure that class is uniquely named.


Consider this:

Foo.h:

@interface Foo: NSObject ... public interface @end 

Foo.m:

@interface __FooSupportClass: NSObject ... interface here ... @end  @implementation __FooSupportClass @end  @interface Foo() @property(retain) __FooSupportClass *__fooSupport; @end  @implementation Foo @synthesize __fooSupport = fooSupport__; ... etc ... @end 

That gives you a private-by-visibility support class only available in your implementation with an instance variable and setter/getter methods on your class that are not visible outside the compilation unit either.

(Note that Objective-C has "instance variables", not "member variables". They are similar, but you'll be better off using the vocabulary of the language.)

like image 86
bbum Avatar answered Sep 18 '22 08:09

bbum


Well you can have "semi-hidden" private methods. You can include an interface file that provides extension methods that is in the implementation file and then just implement the methods declared in there. I was curious about this before and asked a similar question.

Proper Objective-C Helper "Wannabe" Private methods?

like image 39
5StringRyan Avatar answered Sep 21 '22 08:09

5StringRyan