75

how to override a property synthesized getter?

bbum
  • 160,467
  • 23
  • 266
  • 355
Simone D'Amico
  • 2,245
  • 3
  • 16
  • 21
  • 2
    This 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 Answers4

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
  • 132,868
  • 29
  • 267
  • 251
  • 12
    Note 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
  • 12
    Afraid 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
  • 6
    Sure; 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
  • 1
    if 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
  • 3,668
  • 32
  • 28
  • How to specify getter only / setter only? – Pedro Rolo Jan 02 '13 at 11:11
  • 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
  • 69,149
  • 26
  • 113
  • 140
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,117
  • 2
  • 28
  • 53