Mattbettinson
Mattbettinson

Reputation: 27

What is the purpose of instance variables?

I see code like this:

@synthesize dataController = _dataController;

What is the purpose of this in a view controller?

Upvotes: 1

Views: 3763

Answers (4)

Anjali Gaikwad
Anjali Gaikwad

Reputation: 1

An instance is a real-time variable value holder, which contains in the class. An object is a blueprint of the class that handles the instance. Memory allocation and reallocation capabilities in instance

Upvotes: -1

justin
justin

Reputation: 104698

If your class needs to store values, it needs someplace in memory to store this data. An instance variable reserves memory for the data your class needs.

Let's assume you want to add a place for a string or int variable. You can use an instance variable to reserve that memory for the lifetime of the object. Each object will receive unique memory for its variables.

It's much like a C struct:

struct t_something {
  int a; int b;
};

The struct declares two fields (a and b). Each value may be read and written to, and the struct is large enough to hold its fields.

Upvotes: 2

Steve Chiavelli
Steve Chiavelli

Reputation: 51

There is a large amount of info here: iPhone ivar naming convention

One other thing to keep in mind:

Using an instance variable instead of the property in your class bypasses any side effects of the property implementation (retain, copy, etc.) that would normally automatically occur.

This can be especially important if you have written a custom property implementation that you wish to bypass.

Upvotes: 0

m8labs
m8labs

Reputation: 3721

I use it for fast access to data, properties need to write "self." before, vars don't.

Upvotes: 0

Related Questions