Reputation: 6242
Please can somebody explain the difference to me between the following two syntaxes:
@interface BCScanner : CDVPlugin <BCScannerDelegate>
@property (nonatomic, copy) NSString *buttonCallback;
@property (nonatomic, retain) UIView* childView;
@property (nonatomic, retain) UIButton* imageButton;
- (void)createView;
@end
vs.
@interface BCScanner : CDVPlugin <BCScannerDelegate> {
NSString *buttonCallback;
UIView* childView;
UIButton* imageButton;
}
- (void)createView;
@end
Do they both do the same thing?
Upvotes: 0
Views: 201
Reputation: 318804
This block of code:
@interface BCScanner : CDVPlugin <BCScannerDelegate>
@property (nonatomic, copy) NSString *buttonCallback;
@property (nonatomic, retain) UIView* childView;
@property (nonatomic, retain) UIButton* imageButton;
- (void)createView;
@end
defines a class with 3 public properties. These public properties can be used by any other class, including the BSScanner
class itself. This is the proper way to define properties that you want other classes to have access to.
The other block of code:
@interface BCScanner : CDVPlugin <BCScannerDelegate> {
NSString *buttonCallback;
UIView* childView;
UIButton* imageButton;
}
- (void)createView;
@end
defines no properties. Instead it is declaring three private instance variables. These variables can only be accessed by instances of the BCScanner
class. Other class have no direct access to the variables. There is no reason to declare private instance variables in the public header file. These should be moved to the private class extension in the .m file.
Whether you declare public properties or private ivars depends on the need of the values. If other classes should be able to access the values, then declare the public properties in the .h file. If no other class should have access, then declare private ivars in the .m file, not the .h file.
Upvotes: 1
Reputation: 597
You have two choices to define variables.
@interface BCScanner : CDVPlugin <BCScannerDelegate> {
UIButton* imageButton;
}
In this case you can access to variable wihout your class.
[imageButton setTitle:.... ];
However, this variable is private. To access to this variable you must writte getter, or setter to set value.
If define variable with @property, you can access to variable with class:
self.imageButton
or you can use setter from another class:
[AnotherClassVariable setImageButton:(UIButton *)];
In this case is automaticaly declares the accessor and mutator methods for this variable (with @synthesize).
Practical use can be like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"segueId"]){
MyClassWithProperty *dc = segue.destinationViewController;
dc.imageButton = self.imageButton;
//[dc setImageButton:self.imageButton];
}
}
Upvotes: 1
Reputation: 931
Properties are just setters and getters.
For more info check-
https://www.bignerdranch.com/blog/should-i-use-a-property-or-an-instance-variable/
http://rypress.com/tutorials/objective-c/properties
Upvotes: 1