【发布时间】:2011-09-27 23:27:13
【问题描述】:
我定义了一个类,其中前两个属性可以毫无问题地访问。只有UIColor* 是个问题。我想有些东西没有被正确地分配、初始化、保留或释放,并且一直在改变各种事情而没有成功。任何帮助都会很重要。
// PieceScore.h
@interface PieceScore : NSObject {
int pieceCount;
BOOL greatMatch;
UIColor *colorMatched;
}
@property (nonatomic) int pieceCount;
@property (nonatomic) BOOL greatMatch;
@property (nonatomic, retain) UIColor *colorMatched;
-(id) initWithPieceCount:(int)pC withGreatMatch:(BOOL)gM withColorMatched:(UIColor*)cM;
@end
// PieceScore.m
@implementation PieceScore
@synthesize pieceCount, greatMatch, colorMatched;
-(id) init {
return [self initWithPieceCount:0 withGreatMatch:NO withColorMatched:[UIColor clearColor]];
}
-(id) initWithPieceCount:(int)pC withGreatMatch:(BOOL)gM withColorMatched:(UIColor*)cM {
self = [super init];
if (self) {
pieceCount = pC;
greatMatch = gM;
colorMatched = cM;
}
return self;
}
@end
由另一个类初始化并返回如下:
PieceScore* pieceScore = [[[PieceScore alloc] initWithPieceCount:piecesRemoved withGreatMatch:greatMatch withColorMatched:pieceColor] autorelease];
return pieceScore;
注意:(pieceColor 是 UIColor*)
然后,UIColor* 被用在另一个类的方法中:
- (void) labelRender:(UILabel*)label withColor:(UIColor *)color {
// ...
label.textColor = color; // Thread 1: Program received signal: "EXC_BAD_ACCESS".
// ...
}
在调试视图中,我可以看到 color 实际上是作为 UIColor* 传递的,但是在分配给标签的 textColor 属性时出错。
【问题讨论】:
-
当你写 PieceScore*pieceScore = [[[PieceScore alloc] initWithPieceCount:piecesRemoved withGreatMatch:greatMatch withColorMatched:pieceColor] autorelease];返回pieceScore;您从未保留/自动释放颜色对象本身,您只会自动释放 PieceScore。可能这就是它丢失的原因......
-
@Joe :实际上,类似于“label.textColor = [UIColor greenColor];”仍然有效,但以其他方式分配它不起作用。我已经尝试了这里的所有建议,但仍然总是以 SIGABERT 或 EXC_BAD_ACCESS 结束。
标签: iphone objective-c cocoa-touch memory-management