Why declare ivar and property of the same name?

In most projects prior to Xcode 4.4, I realized that developers declared ivar and a property with the same name at the same time. Example

@interface SecondViewController : UIViewController
{
     NSString *string;
}

@property (strong, retain) NSString *string;

So I don’t know why?

+5
source share
2 answers

Just a matter of style and is used for behavior from past compilers.

Now in the new compiler this is not required. As soon as you create a property, an ivar is created behind the scene, and also synthesized.

The compiler synthesizes with an alias as _propertyName, even you can change this default behavior.

@interface AppDelegate : NSObject <NSApplicationDelegate>{
    NSString *bigString;
}
@property(strong)NSString *smallString;
@end

@implementation AppDelegate
@synthesize smallString=bigString;
-(void)awakeFromNib{
    self.smallString=@"hello";
    NSLog(@"%@",self.smallString);
}

@end
+7
source

, . , , , setter/getter.

Xcode 4.4+ @synthesize, setter/getter.

, , , .

, iOS 6 Development:

@interface BIDViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)buttonPressed:(UIButton *)sender;
@end

, , , - , UIButton , .

, , :

BIDViewController *vc = ...;
vc.button = nil;

vc.buttonPressed(mySegmentedControl);

. , 1000 , , (Objective-C , ) .

, Interface Builder:

@implementation BIDViewController ()
{
    IBOutlet UIButton *_button;
}
- (IBAction)_buttonPressed:(UIButton *)sender;

@end

, , .

, , , Apple , , ( , ) .

+1

All Articles