Patrick
Patrick

Reputation: 252

some confusions about " property" in Objective-C

@interface TheViewController : UIViewController
{
    IBOutlet UITableView  *table;
}
@property (nonatomic,retain) IBOutlet UITableView  *table;

or just like this:

@interface TheViewController : UIViewController
{

}
@property (nonatomic,retain) IBOutlet UITableView  *table;

Is it ok like the second one?

And what is the difference ?

And which is recommended ?

Upvotes: 0

Views: 114

Answers (4)

macbirdie
macbirdie

Reputation: 16193

Generally they're equivalent in Objective-C 2.0 environment, but the debugger, for example, won't see the generated ivar as this object's member in the variables view and you'll have to ask for the value by using gdb's command line po command using the accessor method (not the short-form dot notation though).

Upvotes: 0

Scott Little
Scott Little

Reputation: 1939

These two are not quite equivalent even in the ObjC 2.0 environment because of the IBOutlet. That has to be declared on the property. They'd be equivalent like this:

@interface TheViewController : UIViewController
{
    IBOutlet UITableView  *table;
}
@property (nonatomic,retain) UITableView  *table;

@interface TheViewController : UIViewController
{
}
@property (nonatomic,retain) IBOutlet UITableView  *table;

Notice the added IBOutlet in the property definition.

Upvotes: 1

sergio
sergio

Reputation: 69027

Is it ok like the second one?

The two syntaxes are correct, generally speaking.

And what is the difference ?

The second one will also declare the ivar for you, but will only work correctly newer Objective C runtime systems.

And which is recommended ?

good question... I think they are pretty equivalent, provided you can restrict yourself to the newer ObjC runtime systems. For more hints at possible downsides of not declaring ivars explicitly, please have a look at this S.O. post.

Upvotes: 1

Krishnabhadra
Krishnabhadra

Reputation: 34265

In Objective-C 2.0, synthesized properties will automatically create the corresponding ivars as required. So both syntaxes are correct..This article will make it clear for you..

Upvotes: 1

Related Questions