4种颜色中的1种返回错误 [英] 1 out of 4 colors return errors
问题描述
我定义了4种颜色。
@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方法中,我尝试为变量设置一些颜色。
Then in the init method, I try setting some colors to the variables.
- (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:绿色:蓝色:Alpha:。我得到的错误是
The problem I am having is that the program only fails if _teamColor2 is set using colorWithRed: Green: Blue: Alpha:. The error I am getting is
- [UIDeviceRGBColor set]:发送到解除分配的实例0x5f4af80
的消息。我可以改用redColor,它会起作用。有没有理由只有四分之一的人工作不正常?
-[UIDeviceRGBColor set]: message sent to deallocated instance 0x5f4af80 . I can use redColor instead, and it will work. Is there a reason that only one out of four isn't working properly?
推荐答案
无这些应该工作,因为它们都没有被保留( [UIColor colorWithRed:green:blue:alpha:]
返回一个自动释放的实例)。尝试分配给 self.TeamColor
, self.TeamColor2
,等等。
None of those should work, because none of them are being retained ([UIColor colorWithRed:green:blue:alpha:]
returns an autoreleased instance). Try assigning to self.TeamColor
, self.TeamColor2
, and so on instead.
这篇关于4种颜色中的1种返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!