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

ref – http://stackoverflow.com/questions/7057934/property-vs-instance-variable

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.


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


, 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;


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.

ns copying

ref: http://stackoverflow.com/questions/4089238/implementing-nscopying

To implement NSCopying, your object must respond to the -copyWithZone: selector. Here’s how you declare that you conform to it:

Then, in your object’s implementation (your .m file):

What should your code do? First, create a new instance of the object—you can call [[[self class] alloc] init] to get an initialized obejct of the current class, which works well for subclassing. Then, for any instance variables that are a subclass of NSObject that supports copying, you can call [thatObject copyWithZone:zone] for the new object. For primitive types (int, char, BOOL and friends) just set the variables to be equal. So, for your obejct Vendor, it’d look like this:

NSCopying in Base and Sub hierarchy


Base Class

The base is class is straightforward. We only have two member variables, but do not want others to access them. So we declare readonly in @property in the .h file. However, we DO want to modify them in our own implementation. We declare readwrite in our @property in the .m file:


Notice the @protected above the member variables. I did this so that children classes can access these member variables.


Be sure to synthesize them. Its basically getter/setter = iVar

In our case, since we we did not specify an iVar like this:

dayPerformance_PlacingCount is the iVar

makes it run through the setter/getter methods.

So the idea behind copyWithZone is that we need to make a deep copy. Hence everything should have its own address. Staying true to that assertion, we must first dynamically allocate the object to be returned:

We create an empty self class. But wait, What about its member variables? They need to be unique with their own address. So we dynamically create those member variables like so:

Then we have our newly created object’s member variables retain them. the variable copy is our newly created Day variable:

..and then we just return that copy variable.

thus, it gets returned like so:

Thus, yourDay takes on a whole newly allocated Day instance with its own member variables and all.

Base class’s copy method

To use it in your main, you go:

-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 58] – ~~~~~~~~dayOne~~~~~~~~~~~~~
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 59] – dayOne’s address: 0x16dc1f80
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 60] – dayPerformance_PlacingAmount address: 0x16dc1fa0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 61] – dayPerformance_PlacingAmount value: 1204.500000
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 62] – ~~~~~~~~~~~~~~~~~~~~~

-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 66] – ~~~~~~~~dayTwo~~~~~~~~~~~~~
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 67] – dayTwo’s address: 0x16dc22a0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 68] – dayPerformance_PlacingAmount’s address: 0x16dc22e0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 69] – dayPerformance_PlacingAmount’s value: 1204.500000
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 70] – ~~~~~~~~~~~~~~~~~~~~~

As you can see, the Day addresses are different from the copy. As well as the member variables. In this particular case, I only printed out instance variable dayPerformance_PlacingAmount.

Fast Enumeration with Collections

Fast enumeration is an Objective-C’s feature that helps in enumerating through a collection.

Collections in Objective-C

Collections are fundamental constructs. It is used to hold and manage other objects. The whole purpose of a collection is that it provides a common way to store and retrieve objects efficiently.

There are several different types of collections. While they all fulfil the same purpose of being able to hold other objects, they differ mostly in the way objects are retrieved. The most common collections used in Objective-C are:







Fast enumeration Syntax

for (classType variable in collectionObject )

Here is an example for fast enumeration.

static background in uitableview

ref: http://stackoverflow.com/questions/20481118/background-image-uitableview-changes-when-scroll

C++ reference

When you use a reference at the parameter, it gets bound to the outside variable that gets passed in. Namely, in our example, passByRef gets bound to variable. Modifications are made up the same address.

A reference can only be bound ONCE.

Thus, once it gets bound, any modification to that reference takes place outside as well.
So for any assignments inside method test, it happens to variable on the outside as well.

If you try to rebind the reference doing something like:

you’ll get an reassignment error.