For simple properties, you don't need the instance variable declaration or the @synthesize
. The clang compiler will generate both for you by default. So you could write this in the header:
@interface SomeClass : NSObject
@property (nonatomic, copy) NSString *someString;
@end
And the implementation:
@implementation SomeClass
- (void)someMethod {
self.someString = @"Foobar";
}
@end
Avoid direct instance variable access unless you are in the -init
method or overriding the setter. Everywhere else you should use the dot syntax (self.someString
). If you do need access to the instance variable, the default synthesize will create an underscore-prefixed ivar, e.g. _someString
.
Note that for classes with mutable versions like NSString
/NSMutableString
and NSArray
/NSMutableArray
the standard practice is to use a copy
property. If you use strong
on a string or array, the caller might pass in a mutable version and then mutate it from under you, causing hard-to-find bugs.