Jhegs
Jhegs

Reputation: 497

Define private methods that will be used by class methods - Objective C

I want to declare private methods that is useful for other methods of class. If I put a (+) the method is visible (is not private) and If I use (-) a error is throwed in runtime.

@interface SomeClass

+ (int) nameOfMethod;

@end

@implementation SomeClass(PrivateMethods)

+ usefulMethod; // Works but is not private
- otherUsefulMethod; // Compile but throws a error in runtime.

@end

Sorry to all, the second block was a extension class.

Upvotes: 1

Views: 320

Answers (6)

Jason Whitehorn
Jason Whitehorn

Reputation: 13685

There seems to be some confusion about visibility and what + and - mean.

In the case of + and - those simply designate a class versus an instance method, appropriately. Or, in the terminology of other languages, + (void) foo is the same as static void foo() and - (void) bar is the same as void bar() (e.g., - is an instance method).

On the subject of visibility; Strictly speaking Objective-C does not have the concept of public, private, etc like other languages. Any message can be sent to any object, even if it doesn't implement that method.

But, that doesn't mean that you cannot hide the declaration of a method from the public header of a class. So, normally you would have a header file like:

@interface MyObject

- (void) foo;
- (void) secretFoo;

@end 

and you'd have the implementation (.m) as:

@implementation MyObject

- (void) foo {
  NSLog(@"OMG FOO!");
}

- (void) secretFoo{
  NSLog(@"WAT! Don't do that");
}

@end

Now, let's say that the method - (void) secretFoo is, as it's name implies, secret. And, you'd prefer that not just anyone call it. Well, as things are defined your public header broadcasts to the whole world the existence of this secret method. The way to solve this is to remove it from the public header:

@interface MyObject

- (void) foo;

@end 

Now, nobody (generally speaking) will know about your secret method. However, there is one small problem... If you try to call your secretMethod (via normal means) you'll either get a compiler warning or even an error (depending on if you're using ARC). Nobody wants unnecessary warnings, and if you're using ARC it's a complete no-go...

The reason for the warning or error is the same, you'd be trying to invoke secretFoo, but no such method is defined. So, you now need to figure out how to define that method but not in the public header. The way I do it is to modify the implementation file accordingly:

@interface MyObject(Private)
- (void) secretFoo{
@end

@implementation MyObject

- (void) foo {
  NSLog(@"OMG FOO!");
}

- (void) secretFoo{
  NSLog(@"WAT! Don't do that");
}

@end

Now you have your secretFoo method defined for your own use (e.g., no warnings or errors), but it's not broadcast to the world.

Despite me using the term (Private), what I've merely done is implemented a Category, one that is only defined in the implementation file and therefore not generally visible.

Upvotes: 1

Eduardo Iglesias
Eduardo Iglesias

Reputation: 1049

You also could use sharedClasses so you could access that private methods with the same variables and values

And there you access the private methods in the class

The class I created it like NSObject, called "test"

static test *_obj;

@implementation test

+ (test *)sharedTest {
        @synchronized(self) {

            if (_obj == nil) {

                _obj = [[self alloc] init];

                // Allocate/initialize any member variables of the singleton class here
                //_obj.member = @"";
            }
        }
        return _obj;
    }

    - (void)private1 {

    }

And when you call it just like this

[[test sharedTest] private1]

You could access everywhere in your classes and if you store some information will be the same

I hope it helps you

Upvotes: 0

Seva Alekseyev
Seva Alekseyev

Reputation: 61386

In addition to what everyone said, you can define methods in @implementation but not declare them in @interface at all. This way, they will only be visible in the implementation file past the point of introduction. Using them above the point of introduction will emit a compiler warning, but will work. Like this:

@interface C
-(void)a;
-(void)b;
@end

@implementation C
-(void)a
{
    [self x]; //Warning, but works
}

-(void)x
{
    NSLog(@"Woohoo!");
}

-(void)b
{
    [self x]; //No warning
}
@end

Upvotes: 1

Franck
Franck

Reputation: 742

I think you make a confusion.

+ usefulMethod; // + means that it is a class method
- otherMethod; // - means that is an object method

Upvotes: 1

Aaron Hayman
Aaron Hayman

Reputation: 8502

If you're trying to create private methods that only other class methods use, try putting the category in the implementation .m file.

Upvotes: 0

David V
David V

Reputation: 11699

You want to use a class Extension.

In your implementation file (.m) you will have the following:

@interface SomeClass()
+(void)usefulMethod;
-(void)otherUsefulMethod;
@end

@implementation SomeClass
//... normal implementations, including from the class Extension
@end

Upvotes: 0

Related Questions