Aran Mulholland
Aran Mulholland

Reputation: 23935

Objective-C Access @private instance variables from another class

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)?

Upvotes: 1

Views: 3117

Answers (2)

ennuikiller
ennuikiller

Reputation: 46965

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

Upvotes: 4

Lily Ballard
Lily Ballard

Reputation: 185671

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.

Upvotes: 0

Related Questions