ios - objective C下划线变量和自变量不同

标签 ios objective-c variables

我正在研究斯坦福大学 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 以及 initdealloc 方法中使用实例变量(即 _flipCount)(这是 Apple 的建议在那里使用实例变量,因为自定义 getters/setters 可能会做一些不可预测的事情,如果对象还没有完全初始化,或者已经部分销毁,它也可能引发与 KVO 相关的问题。

关于ios - objective C下划线变量和自变量不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27964540/

相关文章:

ios - Parse.com 查询超过 1000 个对象

ios - 本地视频在真机iPhone上播放

iphone - TableView 未从 NSNotification 重新加载

iphone - GameKit/游戏中心-I386

ios - 将 HEX NSString 转换为 ASCII NSString

ios - CGAffineTransformmakeScale 图像质量问题

python向导入的模块添加变量

c - 为什么 printf() 在下面的代码中打印 0 而不是 10?

bash - 初始化变量的不同方法

ios - UIImage 没有成员 fontAwesomeIconWithName