Hide instance variable from header file in Objective C

后端 未结 10 995
梦如初夏
梦如初夏 2020-12-30 14:35

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         


        
相关标签:
10条回答
  • 2020-12-30 14:59

    How about a macro trick?

    Have tested code below

    • have tested with dylibs - worked fine
    • have tested subclassing - Warning! will break, I agree this makes the trick not that useful, but still I think it tells some about how ObjC works...

    MyClass.h

    @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
    

    MyClass.m

    // 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
    
    0 讨论(0)
  • 2020-12-30 15:00

    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.

    0 讨论(0)
  • 2020-12-30 15:00

    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

    0 讨论(0)
  • 2020-12-30 15:03

    Two possibilities:

    1. It could be taking advantage of the modern runtime's ability to synthesize instance variables, as bbum suggested.
    2. The property might not have an underlying instance variable in that class. Properties do not necessarily have a one-to-one mapping with instance variables.
    0 讨论(0)
提交回复
热议问题