为什么对实例变量使用下划线,但不使用其对应的属性? [英] Why do you use an underscore for an instance variable, but not its corresponding property?
问题描述
Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?
我是iphone开发的新手。我正在研究iphone上的录音。从苹果的在这里说的示例程序。它由LevelMeter.h文件组成,其中
I am new to iphone development.I am doing research on voice recording in iphone .I have downloaded the "speak here" sample program from Apple.It consist of LevelMeter.h file, in which
@interface LevelMeter : UIView {
CGFloat _level, _peakLevel;
}
属性设置为
@property CGFloat level;
@property CGFloat peakLevel;
声明一个变量如_level并将其属性设置为level的用法是什么。请解释一下。谢谢。
What is the use of declaring a varible like _level and setting its property as level.Please explain me.Thanks.
推荐答案
提醒
@property
指令等同于声明setter和getter。在 level
的情况下,
Reminder
The @property
directive is equivalent to declaring both a setter and a getter. In the case of level
,
@property CGFloat level;
可替换为
- (CGFloat)level;
- (void)setLevel:(CGFloat)v;
您的问题
对于名为 _level
的变量命名为 level
,为什么要命名一个带有 _
在第一个地方?我不知道。
Your question
Why declare a property named level
for a variable named _level
and why name a variable with a leading _
in the first place? I don't know.
如何运作,在 LevelMeter.m
中回答:
- (CGFloat)level { return _level; }
- (void)setLevel:(CGFloat)v { _level = v; }
这篇关于为什么对实例变量使用下划线,但不使用其对应的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!