Charles Yeung
Charles Yeung

Reputation: 38805

Xcode4 - Declaring an object in header file

For example one, I declare an object inside the interface brace {} like:

@interface testViewController : UIViewController {
    IBOutlet UILabel * myLabel;
}
@property (retain, nonatomic) UILabel *myLabel;

@end

and example two, I declare an object outside the inferface brace {} like:

@interface testViewController : UIViewController {
}
@property (retain, nonatomic) IBOutlet UILabel *myLabel;

@end

I run the code and the result is the same, so I want to ask what is the different for decalare an object inside or outside the interface brace {}?

Thanks

Upvotes: 2

Views: 251

Answers (2)

Abizern
Abizern

Reputation: 150605

The modern Objective-C runtimes (64-bit Mac OS X and iOS) will generate the backing store for your declared properties when you @synthesize them. So you don't need to declare them within the braces.

If you are declaring an iVar that is not a property and will only be used by the class, then they need to be declared. It's a good idea to mark these @private e.g

@interface MyClass : NSObject {
@private
    NSString *privateString;
}
@property (nonatomic, copy) NSString *publicString; // be sure to @synthesize this
@end

Upvotes: 1

unknown
unknown

Reputation: 84

In the second example you only declare a property. Xcode will declare object automatically.

Upvotes: 0

Related Questions