Hi what is the difference between a)
@interface SViewController : UITableViewController{
NSString *_name;
}
@property (nonatomic, retain) NSString *name;
@synthesize name = _name;
b)
@interface SViewController : UITableViewController{
NSString *name;
}
@property (nonatomic, retain) NSString *name;
@synthesize name;
Skip the ivar and just have the property declaration. If you need private members and methods, use a class extension.
An example:
@interface MyObject : NSObject {
}
@property (nonatomic,retain) NSString *publicString;
-(void)publicMethod;
@end
and implemention:
#import "MyObject.h"
@interface MyObject ()
@property (nonatomic,retain) NSString *internalString;
-(void)internalMethod;
@end
@implementation MyObject
@synthesize publicString;
@synthesize internalString;
-(void)publicMethod {}
-(void)internalMethod {}
@end
Note: One downside not declaring an ivar and just have the property, is that gdb in Xcode4 (<= 4.0.1) won't show the property when debugging. Very annoying when writing iOS code, as you can't use LLDB for debugging iOS projects yet.
I prefer a) option, as it prevents you from accidentally forgetting to release or over-release resources. You just always access your variable as self.name and assign either nil or autoreleased value (in case it's a retain property as in your example). In this case if you try to assign a value to "name" without "self." you'll get an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With