Reputation: 7938
This code:
@interface SomeClass : NSObject
@property(getter=myProp) NSString *prop;
@end
@implementation AnotherClass
- (void)test {
SomeClass *s = [[SomeClass alloc] init];
NSString *x = s.prop;
NSString *y = s.myProp;
}
@end
Both x and y are the value of the same property, I got two getters, one is -myProp
and the other is -prop
, so does declaring getter add another getter besides the auto-generated getter?
If this is right, can I use getter=
to generate more than one getter?
Also, how do I remove the auto-generated -prop
getter?
Upvotes: 0
Views: 39
Reputation: 2999
The @property
syntax is really a convenient way to tell Objective-C to synthesize two methods for you:
- (void) setProp:(NSString*)value {
_prop = value ;
}
- (NSString*) prop {
return _prop ;
}
Note that, depending on nonatomic
vs atomic
, it may add threading synchronization in there also.
So, when you write:
NSLog ( object.prop ) ;
You're really writing:
NSLog ( [object prop] ) ;
If you set a getter like you did, it actually creates these methods:
- (void) setProp:(NSString*)value {
_prop = value ;
}
- (NSString*) myProp {
return _prop ;
}
So, these two lines:
NSLog ( object.prop ) ; // prop is a property with a getter, so it uses [object myProp]
NSLog ( object.myProp ) ; // myProp isn't a known property, so it assumes it's [object myProp]
Turn in to:
NSLog ( [object myProp] ) ;
NSLog ( [object myProp] ) ;
Upvotes: 2