Obj-C instance variables and properties (ivar, property)

ref – http://stackoverflow.com/questions/7057934/property-vs-instance-variable
http://stackoverflow.com/questions/9702258/difference-between-properties-and-variables-in-ios-header-file

An instance variable is unique to a class. By default, only the class and subclasses can access it. Therefore, as a fundamental principal of object-oriented programming, instance variables (ivars) are private—they are encapsulated by the class.

By contrast, a property is a public value that may or may not correspond to an instance variable. If you want to make an ivar public, you’d probably make a corresponding property. Declaring (and synthesizing) a property generates getter and setter methods for the instance variable.

But at the same time, instance variables that you wish to keep private should and do not have corresponding properties…so they cannot be accessed from outside of the class.

In Practice

Instance variables declared in .h files are NOT accessible from the outside.

As you can see, pubIVar does not show up and cannot be accessed. Whereas, publicProperty does show up, and we can access it. What we’re accessing in the image is its getter method.

only_property_shows

Of course, instance variables declared in the private extensions of the .m files are obviously also private.

Defining Properties

The approach currently suggested by Apple (in templates) is:

Define property in header file, e.g.:

Then synthesize & declare ivar in implementation:

The last line synthesizes the gameCenter property and asserts that whatever value is assigned to the property will be stored in the __gameCenter ivar. Again, this isn’t necessary, but by defining the ivar next to the synthesizer, you are reducing the locations where you have to type the name of the ivar while still explicitly naming it.

Instance Variables (iVars)

Instance variables, sometimes referred to as

ivars

, are variables declared for a class that exist and hold their value throughout the life of a corresponding class instance (i.e., object). The memory used for instance variables is allocated when an object is first created, and freed when the object is deallocated.

In your interface, you can formally declare an instance variable between the braces, or via @property outside the braces, or both. Either way, they become attributes of the class. The difference is that if you declare @property, then you can implement using @synthesize, which auto-codes your getter/setter for you. The auto-coder setter initializes integers and floats to zero, for example. IF you declare an instance variable, and DO NOT specify a corresponding @property, then you cannot use @synthesize and must write your own getter/setter.

Access to those iVars

@private: The instance variable is only accessible within the class that declares it and other instances of this class type.
@protected: The instance variable is accessible within the class that declares it and the instance methods of any of its subclasses. This is the default scope if a protection level is not specified for an instance variable.
@public: The instance variable is accessible from everywhere.
@package: The instance variable is accessible from any other class instance or function, but outside the package, it is treated as private. This scope can be useful for libraries or framework classes.

Although instance variables provide convenient, direct access to an object’s state, they expose the internals of a class—and this violates the OOP principle of encapsulation. Therefore, instance variables should only be declared when necessary, and the declaration should be in the class implementation, not the public interface.

.m implementation file:

usage for trash:

Also, take note that other classes can’t use ‘helper’ nor ‘trash’ because they are declared in the interface of the implementation file.

.h header file:

If we are to declare the variable in our header file, we can then proceed to go

instObj.trash = 100;

example

We declare a private variable datePickerView. Notice there is no synthesize. We do this because we want to declare our get/set methods. Which means we want explicit get/set definitions.

We want to define an explicit get method.

And we just use it like so. the get method in our example basically make sure there is a newly allocated variable if a previous one is not detected.

Leave a Reply