I came across a library written in Objective C (I only have the header file and the .a binary). In the header file, it is like this:
@interface MyClass : MyS
How about a macro trick?
Have tested code below
@interface MyClass : NSObject {
#ifdef MYCLASS_CONTENT
MYCLASS_CONTENT // Nothing revealed here
#endif
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int extra;
- (id)initWithString:(NSString*)str;
@end
// Define the required Class content here before the #import "MyClass.h"
#define MYCLASS_CONTENT \
NSString *_name; \
int _extra; \
int _hiddenThing;
#import "MyClass.h"
@implementation MyClass
@synthesize name=_name;
@synthesize extra=_extra;
- (id)initWithString:(NSString*)str
{
self = [super init];
if (self) {
self.name = str;
self.extra = 17;
_hiddenThing = 19;
}
return self;
}
- (void)dealloc
{
[_name release];
[super dealloc];
}
@end
For 64 bit applications and iPhone applications (though not in the simulator), property synthesis is also capable of synthesizing the storage for an instance variable.
I.e. this works:
@interface MyClass : MySuperClass
{
//nothing here
}
@property (nonatomic, retain) MyObject *anObject;
@end
@implementation MyClass
@synthesize anObject;
@end
If you compile for 32 bit Mac OS X or the iPhone Simulator, the compiler will give an error.
According to the documentation I've been looking at there is no problem. All you have to do to hide instance variables is to declare them at the start of the @implementation section, inside { ... }. However, I'm a relative newcomer to Objective C and there's a chance I have misunderstood something - I suspect that the language has changed. I have actually tried this system, using XCode 4.2, building code for the iPad, and it seems to work fine.
One of my sources for this idea is the Apple developer documentation at http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocDefiningClasses.html, which gives this pattern:
@implementation ClassName
{
// Instance variable declarations.
}
// Method definitions.
@end
Two possibilities: