Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the visibility of @synthesized instance variables?

If you have a property in your public interface like the following

@interface MyClass : NSObject
@property(strong) NSString *myProp;
@end

And then synthesize it, in effect synthesizing the variable:

@implementation MyClass
@synthesize myProp = _myProp; // or just leave it at the default name..
@end

What is the visibility of the instance variable _myProp? That is, is this considered @public, @protected or @private? I'm guessing since MySubClass could inherit from MyClass then it would also get the properties (naturally), but would it also inherit the instance variable visibility?

What difference does it make if I put the property in a class extension? That would hide the property from subclasses, and I'm guessing the instance variable, too. Is this documented anywhere?

like image 246
jbrennan Avatar asked Dec 14 '11 19:12

jbrennan


People also ask

Should instance variables be public or private?

The instance variables are visible for all methods, constructors, and block in the class. Normally, it is recommended to make these variables private (access level). However, visibility for subclasses can be given for these variables with the use of access modifiers. Instance variables have default values.

Can instance variables be accessed outside the class?

The public instance variables are visible outside the class, and they can be accessed through a reference to the object to which they belong, by means of the field selection operator ``.

What is the scope of an instance variable?

We say the scope of instance variables is the entire class definition. The scope refers to the section of code where a variable can be accessed. hypoteneuse is a parameter variable for testRightTriangle and can only be accessed in the method body, testRightTriangle.

Where are instance variables located?

Instance variables will be stored on the heap. and the object on heap ).


2 Answers

A synthesized ivar is completely invisible to all code that cannot see the @synthesize line (which basically means anything outside of the .m file). It's not @protected, it's not @private, it's simply unknown. With a @private ivar, other code trying to access it will be told that it's private, but with a synthesized ivar, other code trying to access it will be told that the field simply doesn't exist.

As a thought experiment, try imagining a situation where the ivar acted like it was @protected. You make a subclass, and you muck about with the ivar there. Now you go back to the superclass and change @synthesize myProp to @synthesize myProp=foo. What happens in the subclass? When the compiler processes the subclass, it cannot see the @synthesize line, so it would have no idea that you just changed the name of the ivar. In fact, it cannot even tell if the property is backed by an ivar at all, or if it's implemented with custom-written accessor methods. I hope it's obvious why this means that the subclass cannot possibly access the ivar, and neither can any other class.

That said, I'm not quite sure what the compiler does if you write code in the same .m file that tries to access the ivar. I expect it will treat the ivar as @private (since the compiler can, in fact, see that the ivar exists).

Also, none of this has any bearing on the runtime methods. Other classes can still use the obj-c runtime methods to dynamically look up your class's ivar list and muck about with it.

like image 142
Lily Ballard Avatar answered Oct 05 '22 16:10

Lily Ballard


If it is declared in your interface it is virtually public when using the @property declarative. If you want to use @property declaratives and keep them property truly private, you should create a private category in your implementation.

MyClass.h

@interface MyClass : NSObject {
@private
    NSObject* foo;
}
@end

MyClass.m

#import "ClassWithPrivateProperty.h"

@interface MyClass ()
    @property (nonatomic,retain) NSObject* foo; 
@end

@implementation MyClass
@synthesize foo;
// class implementation...
@end
like image 33
Chris Wagner Avatar answered Oct 05 '22 17:10

Chris Wagner