2015-01-15 42 views
-1

我在斯坦福大學ios7的演示應用程序的工作,現在的問題是:目標C下劃線變量和自變量不同

#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數量不會增加,但沒有錯app.But我更改代碼與自我,它的工作原理:

#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的數量。

+0

其平等,但我建議你使用self.property – ErasmoOliveira

回答

3

這行是它不起作用的原因:_flipCount++;

self.flipCount是屬性。 _flipCount是一個實例變量,其中該屬性值是默認存儲的。當您編寫self.flipCount時,您實質上稱此屬性爲吸氣劑:[self flipCount]。當你寫self.flipCount++;時,你可以稱之爲吸氣劑,然後是吸氣劑。非常粗略地可以這樣寫:

[self setFlipCount:[self flipCount] + 1]; 

而你的自定義setter被調用。但是,當您編寫_flipCount++時,您直接訪問實例變量,因此您的自定義設置器將被忽略。這就是爲什麼你的flipLable沒有在第一種情況下更新。

看一看這裏:iOS setters and getters and underscored property names (SO question)

注意,一般來說,你應該使用,而不是實例變量的getter和setter(即self.flipCount)來訪問你想要的數據,即使你有實例變量的直接訪問,因爲getters/setter可能會實現一些特定的行爲。例如,懶惰的初始化,或者像你的情況一樣,UI更新。

你通常只在getter/setter方法和init實例變量(即_flipCount)和dealloc方法(它是一個蘋果的建議,用實例變量存在,因爲自定義的getter/setter方法可能會做一些不可預知的,如果對象工作尚未完全初始化或部分銷燬,也可能引發KVO相關問題)。

+0

感謝告訴我這麼多! –

+0

@zoosuck,不客氣:) – FreeNickname