Objective-C属性和综合逻辑 [英] Objective-C property and synthesize logic

查看:112
本文介绍了Objective-C属性和综合逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在斯坦福大学关于Objective-C和iOS开发的讲座中,实例变量的实际名称是topSpeed是什么?

What is an actual name of instance variable, say, topSpeed, as from lectures of Stanford University about the Objective-C and iOS development?

这是代码:

@property (nonatomic) double topSpeed;

看这段代码,我会认为我已经在类中定义了一个变量topSpeed. 我不明白为什么它会自动声明与变量名相同的getter方法-topSpeed?

Looking at this code I will think that I have defined a variable topSpeed in the class. I can't understand why it will declare automatically the getter method with the name the same as the variable name - topSpeed?

另一个问题是何时使用

@synthesize topSpeed = _topSpeed

如果我们看看@synthesize将产生什么:

And if we look at what the @synthesize will generate:

- (double) setTopSpeed:(double)speed
{
   _topSpeed = speed;
}


- (double) topSpeed
{
   return _topSpeed;
}

这里_topSpeed是什么,什么是topSpeed?我声明了一个变量topSpeed,而不是_topSpeed.如果我不使用property,变量名将是什么?

What is _topSpeed here and what is topSpeed? I have declared a variable topSpeed, not the _topSpeed. What if I don't use property what would the variable name be?

推荐答案

在Obj-C的早期,直到今天,您仍在类的头文件中声明变量,如下所示:

In the earlier days of Obj-C and still today you declared variables in your class's header file like so:

@interface MySubclass : NSObject {
    int varName;
}

然后,您将不得不手动创建setter和getter方法来访问类外部的变量.为了帮助处理内存管理(对对象有用),Apple在Obj-C 2.0中引入了属性,它允许您定义给定变量的访问器.您可以说变量将具有某些属性(例如保留或复制值,具有备用的setter或getter名称等),并且您将其定义为:

Then you would have to manually create setter and getter methods to access the variable outside your class. In order to help deal with memory management (useful for objects), Apple introduced properties in Obj-C 2.0 and it allowed you to define the accessors for a given variable. You could say that a variable would have certain attributes (such as retaining or copying a value, having alternate setter or getter name, etc) and you defined this like:

@property (someAttributes) int varName;

然后,在您的@implementation中,可以@使用给定的属性合成这些属性,编译器将为您的变量生成setter和getter方法.

then in your @implementation you could @synthesize these properties with the given attributes and the compiler would generate setter and getter methods for your variable.

@synthesize varName; // Generates -setVarName: and -varName for you

现在,今天的想法是,您可以远离实现{}部分中的实例变量,而只需声明一个属性和一个synthesize.只是说我们会得到什么

Now, today the idea is that you can move away from implementing the instance variables in the {} section and just declare a property and a synthesize. What we get if we just say

@property (nonatomic) double topSpeed;
@synthesize topSpeed;

是一个名为setTopSpeed:的setter和getter,以及一个名为topSpeed(由编译器创建)的实例变量的topSpeed(用于存储值). @synthesize topSpeed = _topSpeed;背后的想法是实例变量名称将为_topSpeed,但访问器名称仍将为-setTopSpeed:-topSpeed.这有助于提高代码的可读性,因为在代码中说出self.topSpeed或topSpeed之间可能会造成混淆(第一个调用访问器,第二个调用访问器是ivar). _topSpeed将自己与普通变量区分开,并且在调用self.topSpeed(访问器)与_topSpeed(ivar)时也将其显式表示.苹果也正采用这种强调语法,因此不要以为它已经绝种了,因为事实恰恰相反.更新:(请参见汤米的评论)

is a setter and a getter called setTopSpeed: and topSpeed with an instance variable called topSpeed (created by the compiler) to store the value. The idea behind @synthesize topSpeed = _topSpeed; is that the instance variable name will be _topSpeed but the accessor names will still be -setTopSpeed: and -topSpeed. This helps for code readability because there can be confusion between when you say self.topSpeed or topSpeed in your code (the first calls the accessor the second is the ivar). The _topSpeed differentiates itself from normal variables and also makes it explicit when you're calling self.topSpeed (the accessor) vs _topSpeed (the ivar). Apple is moving to this underscore syntax as well so don't think that it's going extinct, because it's quite the opposite. Update: (See Tommy's comment)

它也有助于变量命名冲突.如果您必须实现setTopSpeed:自己,它将看起来像这样:

It also helps with variable naming collisions. If you had to implement setTopSpeed: yourself it would look something like this:

- (void)setTopSpeed:(double)topSpeed {
    _topSpeed = topSpeed; // _topSpeed makes it obvious it's an ivar
}

这篇关于Objective-C属性和综合逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆