-4

In objective-c. I can declare two method. the private (-) method, which is access inside the class, or by the class instance. for example,
in .h file: I declare:

-(void)privateMethodA;
+(void)publicMethodA;

In .m file, I can do

[self privateMethodA];

In other class , like classB .m file; also I can do

ClassA *objA;
[objA privateMethodA];

However, for public method. which is called static method, I guess.

[ClassA publicMethodA];

is enough to access method A in ClassB .m file

In sum up, I can skip the step to declare a instance to access a private method, and just use the [ClassName publicMethodName];

For convenience, I can declare all the method to be public method... So problem is coming, for good program design, what is the difference? Is there something about the memory, such as heap memory, stack memory?

SSH
  • 1,609
  • 2
  • 22
  • 42
seguedestination
  • 419
  • 4
  • 19
  • please read http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods – vadian Aug 10 '15 at 07:02

3 Answers3

5

The - denotes an instance method, the + a class (aka static) method. This has nothing to do with public vs private.

To illustrate the difference between instance and class methods, consider:

NSMutableArray *arr = [NSMutableArray array];
[arr addObject:@"foo"];
[arr addObject:@"bar"];
NSString *foobar = [arr componentsJoinedByString:@""];

Which of array, addObject:, and componentsJoinedByString: are class methods, and which instance methods? Could you swap their places, and if so, how would you call them?

Arkku
  • 41,011
  • 10
  • 62
  • 84
1

First of all, your understanding about private and public methods is totally wrong. Also, static method doesn't have anything related to public methods, although it looks like public method. I really recommend reading some OOP basics.

Private method is the method that can be accessed from inside the class itself, whereas public method is the method that can be accessed from another objects, i.e. used to communicate with outside objects. Both private and public methods can be used on the initialized object, which means, you have to create the class's object in order to use those methods.

As for static method, static methods can be used on uninitialized object, they don't require object alloc init.

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
0

You get it wrong. - and + are not UML-like modifiers. - in ObjC means - instance method declaration (one, that should be sent to object), + is class method (should be sent to Class itself)

@interface Object
- (void) instanceMethod;
+ (void) classMethod;
@end

Somewhere else

Object* a = [[Object alloc] init];
[a instanceMethod];
[Object classMethod];

There is no such thing, like private method in ObjC. Due to its 'dynamic'(check messaging in ObjC) nature it is impossible to check permission for someone to call method in runtime.

The only way to hide method (make it private, that is) - do not expose it in your header file

 //Header Object.h
 @interface Object
 - (void) publicMethod;
 @end

 //Implementation Object.m
 @implementation Object

 - (void) publicMethod {
 /*...*/
    [self privateMethod];  //absolutely legal
 }

 //private method - not exposed in @interface 
 - (void) privateMethod {
 /*...*/
 }
Sergii Martynenko Jr
  • 1,407
  • 1
  • 9
  • 18