xcode @property 和 setter 和 getter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9541828/
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-09-14 23:30:35  来源:igfitidea点击:

@property and setters and getters

objective-cxcode

提问by iggy2012

If I create a @property and synthesize it, and create a getter and setter as well like so:

如果我创建一个@property 并合成它,并像这样创建一个 getter 和 setter:

#import <UIKit/UIKit.h>
{
    NSString * property;
}

@property NSString * property;

--------------------------------

@implementation

@synthesize property = _property

-(void)setProperty(NSString *) property
{
    _property = property;
}

-(NSString *)property
{
    return _property = @"something";
}

Am I correct in assuming that this call

我假设这个电话是否正确

-(NSString *)returnValue
{
    return self.property; // I know that this automatically calls the built in getter function that comes with synthesizing a property, but am I correct in assuming that I have overridden the getter with my getter? Or must I explicitly call my self-defined getter?
}

is the same as this call?

和这个电话一样吗?

-(NSString *)returnValue
{
    return property; // does this call the getter function or the instance variable?
}

is the same as this call?

和这个电话一样吗?

-(NSString *)returnValue
{
    return _property; // is this the same as the first example above?
}

回答by Darren

There are a number of problems with your code, not least of which is that you've inadvertently defined two different instance variables: propertyand _property.

您的代码存在许多问题,尤其是您无意中定义了两个不同的实例变量:property_property.

Objective-C property syntax is merely shorthand for plain old methods and instance variables. You should start by implementing your example without properties: just use regular instance variables and methods:

Objective-C 属性语法只是简单的旧方法和实例变量的简写。您应该首先实现没有属性的示例:只需使用常规实例变量和方法:

@interface MyClass {
    NSString* _myProperty;
}
- (NSString*)myProperty;
- (void)setMyProperty:(NSString*)value;

- (NSString*)someOtherMethod;
@end

@implementation MyClass

- (NSString*)myProperty {
    return [_myProperty stringByAppendingString:@" Tricky."];
}

- (void)setMyProperty:(NSString*)value {
    _myProperty = value; // Assuming ARC is enabled.
}

- (NSString*)someOtherMethod {
    return [self myProperty];
}

@end

To convert this code to use properties, you merely replace the myPropertymethod declarations with a property declaration.

要将此代码转换为使用属性,只需将myProperty方法声明替换为属性声明即可。

@interface MyClass {
    NSString* _myProperty;
}
@property (nonatomic, retain) NSString* myProperty

- (NSString*)someOtherMethod;
@end

...

The implementation remains the same, and works the same.

实现保持不变,工作原理相同。

You have the option of synthesizing your property in your implementation, and this allows you to remove the _myPropertyinstance variable declaration, and the generic property setter:

您可以选择在您的实现中综合您的属性,这允许您删除_myProperty实例变量声明和通用属性设置器:

@interface MyClass
@property (nonatomic, retain) NSString* myProperty;
- (NSString*)someOtherMethod;
@end

@implementation MyClass
@synthesize myProperty = _myProperty; // setter and ivar are created automatically

- (NSString*)myProperty {
    return [_myProperty stringByAppendingString:@" Tricky."];
}

- (NSString*)someOtherMethod {
    return [self myProperty];
}

Each of these examples are identical in how they operate, the property syntax merely shorthand that allows you to write less actual code.

这些示例中的每一个在它们的操作方式上都是相同的,属性语法只是允许您编写较少实际代码的速记。

回答by gregheo

return self.property– will call your overridden getter.

return self.property– 将调用您覆盖的 getter。

return _property;– accesses the property's instance variable directly, no call to the getter.

return _property;– 直接访问属性的实例变量,不调用 getter。

return property;– instance variable.

return property;– 实例变量。

EDIT: I should emphasize that you will have twodifferent NSString variables -- propertyand _property. I'm assuming you're testing the boundaries here and not providing actual production code.

编辑:我应该强调您将有两个不同的 NSString 变量 -property_property. 我假设您在这里测试边界而不提供实际的生产代码。

回答by Anurag Bhakuni

above answer elaborate almost all the thing , i want to elaborate it little more.

上面的答案几乎说明了所有事情,我想再详细说明一下。

// older way

// 老方法

@interface MyClass {
    NSString* _myProperty; // instance variable
}
- (NSString*)myProperty; // getter method
- (void)setMyProperty:(NSString*)value;//setter method

@end

@结尾

the instance variable can not be seen outside this class , for that we have to make getter and setter for it. and latter on synthesis it in .m file

实例变量不能在这个类之外看到,为此我们必须为它制作getter和setter。后者在.m文件中合成

but now

但现在

we only used

我们只用过

  @property(nonatomic) NSString *myProperty;

the @propertyis an Objective-C directive which declares the property

@property是一个声明属性的Objective-C指令

-> The "`nonatomic`" in the parenthesis specifies that the property is non-atomic in nature.
-> and then we define the type and name  of our property.
-> prototyping of getter and setter method

now go to .m file

现在转到 .m 文件

previously we have synthesis this property by using @synthesis, now it also not required it automatically done by IDE.

以前我们通过 using 综合了这个属性@synthesis,现在它也不需要由 IDE 自动完成。

little addition : this `@synthesis` now generate the getter and setter(if not readonly) methods.