Objective C强调变量和自变量不同

时间:2015-01-15 13:29:07

标签: ios objective-c variables

我正在研究stanford university 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数字不会添加,但应用程序没有任何问题。但是我使用self更改代码,它可以工作:

#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数量的两个变量的区别是什么。

1 个答案:

答案 0 :(得分:4)

这一行是它不起作用的原因:_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 / setter可能会实现一些具体行为。例如,延迟初始化,或者像在您的情况下,UI更新。

您通常只使用getter / setter中的实例变量(即_flipCount)以及initdealloc方法(Apple建议在那里使用实例变量,因为自定义getters / setter可能会做一些不可预测的事情,如果对象尚未完全初始化或被部分破坏,它也可能引发与KVO相关的问题。)