Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @interface declaration and @property declaration

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.

like image 212
Nigel Avatar asked Jan 29 '10 03:01

Nigel


People also ask

What is declare interface?

Declaring InterfacesAn interface adds the functionality of strong type checking for your functions, variables, or the class that is implementing the interface. Interfaces make sure that everything is implemented as expected.

Can we define variable in interface C#?

No you can not declare variable in interface. No, we can't declare variables, constructors, properties, and methods in the interface.


2 Answers

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

like image 170
Steve Harrison Avatar answered Oct 03 '22 20:10

Steve Harrison


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.

like image 34
Rob Keniger Avatar answered Oct 03 '22 20:10

Rob Keniger