1

I've been looking at this question and came across the following code in one of the answers

@interface MyClass : NSObject
{
    @private
    int someVar;  // Can only be accessed by instances of MyClass

    @public
    int aPublicVar;  // Can be accessed by any object
}
@end

Is there anyway to access someVar from any other class (including derived classes)?

Community
  • 1
  • 1
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228

2 Answers2

4

As with most dynamic languages you can get at this information in Objective C, however, it is painful. Look here for an example.

Community
  • 1
  • 1
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
  • You *can*, but you *shouldn't*. If it's been declared `@private`, it's usually for a good reason. –  Dec 18 '11 at 22:46
  • 5
    @darvids0n I'm not questioning whether you should or shouldn't, that's pretty obvious, I'm just wondering if you can. – Aran Mulholland Dec 18 '11 at 22:55
0

No. That's exactly what @private means. The compiler explicitly disallows any other class (even subclasses) from accessing that ivar.

@public means anyone can access the ivar, and @protected means subclasses can access it but other classes can't.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347