Reputation: 3015
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;
Upvotes: 3
Views: 173
Reputation: 1983
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.
Upvotes: 2
Reputation: 1126
I never use instance variables anymore, except sometimes when debugging so that I can take a look at a value. The rest of the time I just use properties and access them through the accessor methods that are created when you synthesize the property.
For a property called "myVariable" when you synthesize you automatically get a getter and setter method. The getter is simply the name of your variable, in this case "myVariable" and it would be accessed like this:
someOtherVariable = [self myVariable];
The setter is made by prepending the variable name with the word "set" and capitalizing the first letter of your variable which is why you must create your variables using camelCase. The setter in this example looks like this:
[self setMyVariable:someOtherVariable];
Note that I use the Objective-C message syntax as opposed to the dot syntax. I highly recommend this as it is a fundamental part of Objective-C and I find is much more clear. If you insist on using the dot syntax your getter would look like this:
someOtherVariable = self.myVariable;
and your setter would look like this:
self.myVariable = someOtherVariable;
Upvotes: 0
Reputation: 2268
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.
Upvotes: 1
Reputation: 677
Whenever a local variable has the same name as iVar ,approach A) avoids the below warning.
"local declaration of 'object' hides instance variable."
Upvotes: 0
Reputation: 1599
There is no effective difference. It all comes down to variable naming conventions. Private variables often are prefixed with an underscore ('_') which helps with code readability. If you're working with a group it's important for everyone to agree to the same naming conventions. As long as you're consistant you there is no "right" answer.
Normally the first letter of a variable is camel case (ex. firstWordLowerCase). Name should be name in your example. Since it is a readwrite property my personal preference is to not use the underscore. If it had been declared readonly then I would have used it.
Above all be consistant.
Upvotes: 0