ios 如何覆盖@synthesized getter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5047399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:56:41  来源:igfitidea点击:

How to override @synthesized getters?

iphoneobjective-cios

提问by Simone D'Amico

how to override a property synthesized getter?

如何覆盖属性合成的getter?

采纳答案by diadyne

Inside of your property definition you can specify getter and setter methods as follows:

在您的属性定义中,您可以指定 getter 和 setter 方法,如下所示:

@property (nonatomic, retain, getter = getterMethodName, setter = setterMethodName) NSString *someString;

You can specify the getter only, the setter only, or both.

您可以仅指定 getter、仅 setter 或同时指定两者。

回答by Ole Begemann

Just implement the method manually, for example:

只需手动实现该方法,例如:

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

The compiler will then not generate a getter method.

编译器将不会生成 getter 方法。

回答by stefanB

Just implement your own getter and the compiler will not generate one. The same goes for setter.

只需实现您自己的 getter,编译器就不会生成。二传手也是如此。

For example:

例如:

@property float value;

is equivalent to:

相当于:

- (float)value;
- (void)setValue:(float)newValue;

回答by user1105951

I just want to add, I was not able to override BOOL property with getter/setter, until I add this :

我只想补充一点,在我添加以下内容之前,我无法使用 getter/setter 覆盖 BOOL 属性:

@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;
}