我正在研究斯坦福大学 ios7 的演示应用程序,这是问题所在:
#import "YSHViewController.h"
@interface YSHViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLable;
@property (nonatomic) int flipCount;
@end
@implementation YSHViewController
- (void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
_flipsLable.text =[NSString stringWithFormat:@"Flips: %d",_flipCount]; // not working
NSLog(@"%@", _flipsLable.text);
NSLog(@"%@", self.flipsLable.text);
self.flipsLable.text = [NSString stringWithFormat:@"Flips: %d",_flipCount];
}
- (IBAction)touchCardButton:(UIButton *)sender {
//if (sender.currentTitle.length != 0) {
if ([sender.currentTitle length]) {
//UIImage *cardImage = [UIImage imageNamed:@"cardback"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
forState:UIControlStateNormal];
[sender setTitle:@"" forState:UIControlStateNormal];
} else {
//UIImage *cardImage = [UIImage imageNamed:@"cardfront"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
forState:UIControlStateNormal];
[sender setTitle:@"A♣︎" forState:UIControlStateNormal];
}
_flipCount++;
NSLog(@"%i",_flipCount); //
// self.flipCount++;
}
flipLable 数字不会添加,但应用程序没有问题。但我用自己更改了代码,它有效:
#import "YSHViewController.h"
@interface YSHViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLable;
@property (nonatomic) int flipCount;
@end
@implementation YSHViewController
- (void)setFlipCount:(int)flipCount
{
// _flipCount = flipCount;
// _flipsLable.text =[NSString stringWithFormat:@"Flips: %d",_flipCount]; // not working
// NSLog(@"%@", _flipsLable.text);
// NSLog(@"%@", self.flipsLable.text);
_flipCount = flipCount;
self.flipsLable.text = [NSString stringWithFormat:@"Flips: %d",self.flipCount];
}
- (IBAction)touchCardButton:(UIButton *)sender {
//if (sender.currentTitle.length != 0) {
if ([sender.currentTitle length]) {
//UIImage *cardImage = [UIImage imageNamed:@"cardback"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
forState:UIControlStateNormal];
[sender setTitle:@"" forState:UIControlStateNormal];
} else {
//UIImage *cardImage = [UIImage imageNamed:@"cardfront"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
forState:UIControlStateNormal];
[sender setTitle:@"A♣︎" forState:UIControlStateNormal];
}
// _flipCount++;
// NSLog(@"%i",_flipCount); //
self.flipCount++;
}
那么,设置fliCount个数的两个变量有什么区别。
最佳答案
这一行是它不起作用的原因:_flipCount++;
。
self.flipCount
是一个属性。 _flipCount
是一个实例变量,默认情况下存储该属性值。当您编写 self.flipCount
时,您实质上是为此属性调用了一个 getter:[self flipCount]
。当您编写 self.flipCount++;
时,您调用了一个 getter,然后是一个 setter。很粗略的可以这样写:
[self setFlipCount:[self flipCount] + 1];
您的自定义 setter 会被调用。但是,当您编写 _flipCount++
时,您直接访问实例变量,因此您的自定义 setter 将被忽略。这就是为什么您的 flipLable
在第一种情况下没有更新的原因。
看看这里:iOS setters and getters and underscored property names (SO question)
请注意,通常您应该使用 getter 和 setter(即 self.flipCount
)而不是实例变量来访问您想要的数据,即使您可以直接访问实例变量,因为 getter/setters 可能会实现一些特定的行为。例如,延迟初始化,或者像您的情况一样,UI 更新。
您通常只在 getter/setter 以及 init
和 dealloc
方法中使用实例变量(即 _flipCount
)(这是 Apple 的建议在那里使用实例变量,因为自定义 getters/setters 可能会做一些不可预测的事情,如果对象还没有完全初始化,或者已经部分销毁,它也可能引发与 KVO 相关的问题。
关于ios - objective C下划线变量和自变量不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27964540/