0

I have a class which has some methods that are only to be used within the class itself. These methods exist because I have a three-step process for the graphics work I'm doing, but I only want instances of the class to access the final result of those calculations, in a simplified example:

#import <Foundation/Foundation.h>

@interface GraphicsWorld : NSObject

@property(nonatomic, strong) NSMutableArray *objects;
@property(nonatomic, strong) NSMutableArray *adjustedObjects

/* three methods I'll never use outside of this class

 I want to find a way to get replace these methods.
 */
-(void) calcTranslation;
-(void) calcRotation;
-(void) calcPerspective;

/* the one method I'll use outside of this class */
-(NSMutableArray *) getAdjustedObjects;

@end

I could define c-functions just outside of my implementation for this, but then they wouldn't have access to the properties:

#import <Foundation/Foundation.h>
#import "GraphicsWorld.h"

void calcTranslation()
{
    // I'm useless because I can't access _objects.
}

void calcRotation()
{
    // Hey, me too.
}

void calcPerspective()
{
    // Wow, we have a lot in common.
}

@implementation GraphicsWorld

-(NSMutableArray *) getAdjustedObjects
{
    calcTranslation();
    calcRotation();
    calcPerspective();

    return adjustedObjects;
}

@end
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83

3 Answers3

0
  1. Make C-style functions (as you've shown) that take arguments and return values.
  2. Make private Objective-C-style methods.

In addition to your @implementation section in the .h file, you can also have one in your .m file, which is private. Just as you declare methods and properties in the .h file's @implementation, you can do the same in the .m.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • > Make C-style functions (as you've shown) that take arguments and return values. Well, this won't work because then I can't access my ivars; unless, you're suggesting that I pass my ivars in as arguments to those methods. Are you? I've considered this, but I wondered if it was bad practice or not. – michaelsnowden Jan 26 '14 at 01:07
  • If you're talking about `objects` and `adjustedObjects`, these has public accessors, and can be accessed by passing `self` to the function. If you're talking about instance variables which aren't public, you can pass them as arguments. – nhgrif Jan 26 '14 at 02:43
0

Unless I'm misunderstanding your question, it sounds like you just want to hide your methods from being public? If so, just delete them from the header. You no longer need to declare methods in advance in objc (Xcode). The compiler will just find them internally now.

D.C.
  • 15,340
  • 19
  • 71
  • 102
  • 1
    So I can just add my methods into the implementation without ever declaring them? Whhaaaat. – michaelsnowden Jan 26 '14 at 01:09
  • @doctordoder You never needed to add methods to the .h file. You only needed to add methods to a .h if other classes needed to access the methods. – rmaddy Jan 26 '14 at 01:55
  • I like to declare 'em in a class extension at the top of the .m – bbum Jan 26 '14 at 02:35
  • 1
    Ya, I do what @bbum does sometimes, to help with some self documenting, but if the methods are really private, I find they just add more typing. – D.C. Jan 26 '14 at 03:06
0

A method can be called whether it is declared private, or not put in the header file; due to the nature of Objective-C hiding methods is hard.

Hiding functions is a lot easier, just declare them static. To access the current instance you just pass in a reference to it - i.e. exactly what Objective-C does behind the scenes.

So for example:

void calcTranslation(GraphicsWorld *self)
{
    // Access properties, instance variables, call instance methods etc.
    // by referencing self. You *must* include self to reference an
    // instance variable, e.g. self->ivar, as this is not a method the
    // self-> part is not inferred.
}

and to call it:

-(NSMutableArray *) getAdjustedObjects
{
    calcTranslation(self);
    ...
CRD
  • 52,522
  • 5
  • 70
  • 86
  • This is pretty much never done and access ivars via `self->` is really truly never done. Just use a class extension, declare the methods, and be done with it. – bbum Jan 26 '14 at 02:33
  • @bbum - Well both are done, and if it meets a need there is nothing wrong with it. However simply not publicising methods (with or without using an extension) suits the more common need. Horses for courses. – CRD Jan 26 '14 at 08:50
  • 2
    Not following convention and employing new patterns with no distinct advantage leads to codebases that are difficult to maintain and difficult to refactor. It also incurs a significant cost when adding new people to the project. – bbum Jan 26 '14 at 19:01
  • @bbum - Of course, but sometimes it meets a need, horses for courses. Let's leave it at that, have a nice day :-) – CRD Jan 26 '14 at 20:39