views:

255

answers:

4

I'm new to C, new to objective C. For an iPhone subclass, Im declaring variables I want to be visible to all methods in a class into the @interface class definition eg

@interface myclass : UIImageView {
    int aVar;
}

and then I declare it again as

@property int aVar;

And then later I

@synthesize aVar;

Can you help me understand the purpose of three steps? Am I doing something unnecessary?

Thanks.

A: 

@interface declares the instances variables of a class in obj-c. You need it to create an instance variable. However the variable is not visible outside the class by default (as the field is by default protected).

@property tells the compiler to specify a particular property accessor (get/set) method. However, you will need to use @synthesize to actually have the compiler generate the simple accessors automatically, otherwise you are expected to create them on your own.

futureelite7
A: 

Classes can have instance variables (ivars). These are in the first section, and are only visible to code in that class, not any outside code. I like to prefix them with an underscore to show their internal-ness. In low level terms, the ivars are added as an additional member to the struct that the class you are creating uses internally.

The second declaration, @property, is a declared property. It is not required (except when you are using @synthesize), but it helps other programmers (and the compiler!) know that you are dealing with a property, and not just two methods -setAVar and -aVar, which is the alternative way of doing this.

Thirdly, the @synthesize actually creates the methods to set and access the property from outside the class. You can replace this with your own setter and getter methods, but only do that if you need to, as the built-in ones have some features that you would otherwise have to code yourself. In fact, using the @synthesize aVar = _someVariable; syntax, you can have your property actually reference a differently named instance variable!

Short version: The @property is just a hint to the compiler and other programmers that you are making a property and not just getter/setter methods. The instance variables are internal to the class, and otherwise cannot be normally accessed from outside it. The @synthesize just creates simple getters and setters for you, to go with your @property, but you can also just code those getters and setters yourself, like any other method.

chpwn
Thanks - Is it recommended to use synthesized methods inside the class that owns them? eg should I use [self setaVar:123]; rather than aVar=123?
Nigel
@Nigel: I think it's generally best to use accessors and mutators within the class. You might find my question http://stackoverflow.com/questions/1551966/properties-and-instance-variables-in-objective-c helpful.
Steve Harrison
A: 

This declares an instance variable in your object:

@interface myclass : UIImageView {
    int aVar;
}

Instance variables are private implementation details of your class.

If you want other objects to be able to read or set the value of the instance variable (ivar), you can declare it as a property:

@property int aVar;

This means that the compiler expects to see setter and getter accessor methods for the property.

When you use the @synthesize keyword, you are asking the compiler to automatically generate setter and getter accessor methods for you.

So, in this case the compiler will generate code similar to this when it encounters the @synthesize keyword:

- (int) aVar
{
    return aVar;
}

- (void)setAVar:(int)someInt
{
    aVar = someInt;
}

By default on the iPhone (and on the 32-bit runtime on the Mac), @synthesize requires an instance variable to be present in order to store the property's value. This ivar is usually named the same as the property, but doesn't have to be, for instance you could do this:

@interface myclass : UIImageView {
    int aVar;
}

@property int someValue;

@synthesize someValue = aVar;

Neither @synthesize nor @property are actually required, you can create your own getter and setter methods, and as long as you create them using Key-Value Coding-compliant syntax, the property will still be usable.

The requirement for an ivar to be present as well as the @property declaration is due to the fragile base class limitation of the 32-bit Objective-C runtime on both the Mac and iPhone. With the 64-bit runtime on the Mac you don't need an ivar, @synthesize generates one for you.

Note that there are numerous keywords you can use with your @property declaration to control what sort of synthesized accessor code is created, such as readonly for a getter-only accessor, copy, atomic, nonatomic and so on. More information is in the Objective-C 2.0 Programming Language documentation.

Rob Keniger
Thanks - why do I need setter and getter methods (assuming they are the generic ones created by synthesize)? Are they required in order for a property to be visible to external classes?
Nigel
@Nigel: Yes—if you don't have setter and getter methods, no other classes will be able to access or change the instance variable (instance variables are private).
Steve Harrison
A: 

Here, you're declaring an instance variable named aVar:

@interface myclass : UIImageView {
    int aVar;
}

You can now use this variable within your class:

aVar = 42;
NSLog(@"The Answer is %i.", aVar);

However, instance variables are private in Objective-C. What if you need other classes to be able to access and/or change aVar? Since methods are public in Objective-C, the answer is to write an accessor (getter) method that returns aVar and a mutator (setter) method that sets aVar:

// In header (.h) file

- (int)aVar;
- (void)setAVar:(int)newAVar;

// In implementation (.m) file

- (int)aVar {
    return aVar;
}

- (void)setAVar:(int)newAVar {
    if (aVar != newAVar) {
        aVar = newAVar;
    }
}

Now other classes can get and set aVar via:

[myclass aVar];
[myclass setAVar:24];

Writing these accessor and mutator methods can get quite tedious, so in Objective-C 2.0, Apple simplified it for us. We can now write:

// In header (.h) file

@property (nonatomic, assign) int aVar;

// In implementation (.m) file

@synthesize aVar;

...and the accessor/mutator methods will be automatically generated for us.

To sum up:

  • int aVar; declares an instance variable aVar

  • @property (nonatomic, assign) int aVar; declares the accessor and mutator methods for aVar

  • @synthesize aVar; implements the accessor and mutator methods for aVar

Steve Harrison
great explanation (though Rob and chpwn are also very clear), thankyou all.
Nigel
@Nigel: Glad to be of assistance! :)
Steve Harrison
Note that with the iPhone OS and 64 bit Mac OS X, the environment variable declaration is optional.
bbum