2011-02-28 75 views
1

我有4種顏色定義。4種顏色中有1種返回錯誤

@interface Global : NSObject { 

    UIColor *_EnemyColor; 
    UIColor *_EnemyColor2; 
    UIColor *_TeamColor; 
    UIColor *_TeamColor2; 

} 

@property (nonatomic, retain) UIColor *EnemyColor; 
@property (nonatomic, retain) UIColor *EnemyColor2; 
@property (nonatomic, retain) UIColor *TeamColor; 
@property (nonatomic, retain) UIColor *TeamColor2; 

@end 

@synthesize EnemyColor = _EnemyColor; 
@synthesize EnemyColor2 = _EnemyColor2; 
@synthesize TeamColor = _TeamColor; 
@synthesize TeamColor2 = _TeamColor2; 

然後在init方法,我嘗試設置一些顏色的變量。

- (id)init { 
if (self = [super init]) { 
    //******************************************************************* 
    _TeamColor = [UIColor colorWithRed:0.70196078 green:0.70196078 blue:0.70196078 alpha:1.0]; 
    //Everything works, if this line is commented out 
    _TeamColor2 = [UIColor colorWithRed:0.82352941 green:0.81960784 blue:0.83921569 alpha:1.0]; 
    switch (arc4random() % 4) { 
     case 0: 
      _EnemyColor = [UIColor colorWithRed:0.50196078 green:0.47843137 blue:0.41568627 alpha:1]; 
      _EnemyColor2 = [UIColor colorWithRed:0.63529412 green:0.57647059 blue:0.44705882 alpha:1]; 
      break; 
     case 1: 
      _EnemyColor = [UIColor colorWithRed:0.72156863 green:0.59607843 blue:0.37254902 alpha:1]; 
      _EnemyColor2 = [UIColor colorWithRed:0.81568627 green:0.73333333 blue:0.51764706 alpha:1]; 
      break; 
     case 2: 
      _EnemyColor = [UIColor colorWithRed:0.75686275 green:0.47843137 blue:0.23529412 alpha:1]; 
      _EnemyColor2 = [UIColor colorWithRed:0.85098039 green:0.56470588 blue:0.35686275 alpha:1]; 
      break; 
     case 3: 
      _EnemyColor = [UIColor colorWithRed:0.45882353 green:0.6 blue:0.70196078 alpha:1]; 
      _EnemyColor2 = [UIColor colorWithRed:0.57254902 green:0.65882353 blue:0.74117647 alpha:1]; 
      break; 
    } 

我遇到的問題是,如果_teamColor2使用colorWithRed設定的程序只失敗:綠:藍:阿爾法:.我得到的錯誤是

- [UIDeviceRGBColor set]:發送到解除分配的實例0x5f4af80的消息 。我可以使用redColor來代替,並且它可以工作。是否有一個原因,只有四分之一不能正常工作?

回答

6

這些都不應該起作用,因爲它們都不被保留([UIColor colorWithRed:green:blue:alpha:]返回自動釋放實例)。嘗試分配到self.TeamColorself.TeamColor2,等等。