Do I need to declare variables when using @property?


#1

Hi to all,

I just came across a question: My (working) class Stockholding is declared in Stockholding.h like this:

[code]#import <Foundation/Foundation.h>

@interface Stockholding : NSObject

@property float purchaseSharePrice, currentSharePrice;
@property int numberOfShares;

  • (float)costInDollars;
  • (float)valueInDollars;
    @end
    [/code]

I omitted the declaration of the three variables in the @interface section because they are already defined through @property. The class works and my question is: Is omitting variable declarations okay when the are declared via @property?

Thanks and happy coding

iFlash


#2

Apparently so. Here’s an interesting blog post I stumbled across yesterday (seems like a useful website in general):

raptureinvenice.com/an-open-lett … -in-ios-6/


#3

@property in the interface along with @synthesize in the implementation create synthesized accessor methods. They create the setter and getter methods for you. Instance variables that have synthetic methods do not need to be declared in the interface, the @property / @ synthesize takes care of it. :smiley:


#4

I realized later that I answered the question in your post, not the question in your title. Title answer: apparently not.


#5

So would this be an improvement in practice? That blogger seems to think so…


#6

Definitely an improvement: the more grunt work the compiler can do for the programmer, the fewer opportunities for errors (and the less nuisance typing).

The @synthesize feature has annoyed me ever since I started poking at Objective-C. Seems like such a gratuitous oversight to require it, but perhaps there’s some corner case where generating the methods without the programmer explicitly authorizing it would cause problems.


#7

In the section “Declared Properties” of the “The Objective-C Programming Language” in Apple documentation, you can get details about using @property and @synthesize keywords.
The Objective-C declared properties feature provides a simple way to declare and implement an object’s accessor methods. Is like automatic implemented properties in C#, since version 3.0. Is a “sugar” syntax.

There are two parts to a declared property, its declaration and its implementation.

Property Declaration
A property declaration begins with the keyword @property.

Note: Until this chapter, the book does not give information about attributes.

You can think of a property declaration as being equivalent to declaring two accessor methods. Thus

is equivalent to:

[code]- (float)myProperty;

  • (void)setMyProperty:(float)value;[/code]

Property Implementation Directives

You can use the @synthesize and @dynamic directives in @implementation blocks to trigger specific compiler actions. Note that neither is required for any given @property declaration.

You use the @synthesize directive to tell the compiler that it should synthesize the setter and/or getter methods for a property if you do not supply them within the @implementation block. The @synthesize directive also synthesizes an appropriate instance variable if it is not otherwise declared.