caribbean
caribbean

Reputation: 748

Proper declaration of outlets in Objective-C

I am a little bit confuse about the this:

Why do some programmers use this at their .h file

{
IBOutlet UIScrollView* scrollView;
IBOutlet UIPageControl* pageControl;
}
@property (nonatomic, retain) UIView *scrollView;
@property (nonatomic, retain) UIPageControl* pageControl;

Instead just this...

    @property (nonatomic, retain) UIView *scrollView;
    @property (nonatomic, retain) UIPageControl* pageControl;

I am new to XCode and Objective C , I will be glad if there is anyone who can explain me this. Thanks.

Upvotes: 1

Views: 111

Answers (3)

Cyrille
Cyrille

Reputation: 25144

  1. Outlets are properties

  2. properties need an ivar (unless some very specific case)

  3. Since Xcode 4.3 or 4.4, ivars are automatically added and synthesized by the compiler if absent.

Upvotes: 0

Antonio MG
Antonio MG

Reputation: 20410

In recents versions of the toolchain, it's enough just doing this:

@property (nonatomic, retain) IBOutlet UIView *scrollView;

(If you dont put the IBOutlet you wont be able to connect it to the interface builder)

Some people still do it in the old way, some of them just tradition, some dont know it can be skipped, or it's because they are just old projects.

Upvotes: 2

mipadi
mipadi

Reputation: 410722

It was only relatively recently that you could declare properties without a corresponding instance variable (the compiler will create the backing instance variable for you now if necessary, but that wasn't always the case).

Upvotes: 1

Related Questions