how to override a property synthesized getter?
Asked
Active
Viewed 3.8k times
76
-
2This is neither iPhone or iOS specific. It isn't even specific to any platform or framework. – Feb 20 '11 at 10:06
-
"Override" has a specific meaning in OOP and Objective-C. Alas, you probably mean something else: "How to prevent synthesizing a getter and instead implement it manually?" If I'm interpreting you correctly you did accept the wrong answer. – Nikolai Ruhe Feb 24 '15 at 17:47
-
2@user142019: Why does that matter? – Stewart Macdonald Jun 06 '15 at 03:50
4 Answers
76
Just implement the method manually, for example:
- (BOOL)myBoolProperty
{
// do something else
...
return myBoolProperty;
}
The compiler will then not generate a getter method.

Ole Begemann
- 135,006
- 31
- 278
- 256
-
12Note that if your `@property` is `atomic` (the default), you cannot correctly mix an `@synthesize`d getter/setter with a manually written getter/setter. – bbum Feb 18 '11 at 23:26
-
12Afraid of what? `atomic` isn't *that* useful; it does nothing to guarantee data integrity in a threaded application. It just prevents an app from crashing due to an obvious race condition. If you are relying on `atomic` heavily, you are almost assuredly doing it wrong... :) – bbum Feb 19 '11 at 01:13
-
2@bbum: could you elaborate on what you mean by "you cannot correctly mix a synthesized getter/setter with a manually written getter/setter" when the property is atomic? I don't understand what one has to do with the other. Thanks. – Ole Begemann Feb 19 '11 at 01:56
-
6Sure; http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties may be helpful. Specifically, since the locking mechanism to implement `atomic` is not exposed, there is no way you can manually implement just the setter or just the getter and ensure atomicity with an `@synthesize`d `atomic` setter/getter. – bbum Feb 19 '11 at 04:13
-
1if you write a getter method like that, will the property automatically be readonly? if not, where does the data get stored if the property is assigned a value? – matthias_buehlmann Feb 05 '14 at 16:15
55
Inside of your property definition you can specify getter and setter methods as follows:
@property (nonatomic, retain, getter = getterMethodName, setter = setterMethodName) NSString *someString;
You can specify the getter only, the setter only, or both.

diadyne
- 4,038
- 36
- 28
-
-
In my Xcode project I do not need to @synthesize someString, in this case how can I set its value? Xcode error says that is an undeclared property.. Thx – Mr. Frank May 16 '13 at 15:20
-
5@Mr.Frank: Xcode now does the synthesizing of properties automatically by default. This was added in one of the Xcode versions (4.5?). So, basically, you don't have to `@synthesize` your variables anymore, you just need the `@property`, and in there you just need to specify your getter and/or setter. – matsr Jul 23 '13 at 21:10
43
Just implement your own getter and the compiler will not generate one. The same goes for setter.
For example:
@property float value;
is equivalent to:
- (float)value;
- (void)setValue:(float)newValue;

stefanB
- 77,323
- 27
- 116
- 141
10
I just want to add, I was not able to override BOOL property with getter/setter, until I add this :
@synthesize myBoolProperty = _myBoolProperty;
so the complete code is :
in header file :
@property BOOL myBoolProperty;
in implementation file :
@synthesize myBoolProperty = _myBoolProperty;
-(void)setMyBoolProperty:(BOOL) myBoolPropertyNewValue
{
_myBoolProperty = myBoolPropertyNewValue;
}
-(BOOL) myBoolProperty
{
return _myBoolProperty;
}

user1105951
- 2,259
- 2
- 34
- 55