How to override @synthesized getters?

前端 未结 4 500
逝去的感伤
逝去的感伤 2020-12-24 00:11

how to override a property synthesized getter?

相关标签:
4条回答
  • 2020-12-24 00:39

    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.

    0 讨论(0)
  • 2020-12-24 00:56

    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;
    
    0 讨论(0)
  • 2020-12-24 00:57

    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;
    }
    
    0 讨论(0)
  • 2020-12-24 01:00

    Just implement the method manually, for example:

    - (BOOL)myBoolProperty
    {
        // do something else
        ...
        return myBoolProperty;
    }
    

    The compiler will then not generate a getter method.

    0 讨论(0)
提交回复
热议问题