我很难过。我没有任何错误甚至警告。我在调用它的行上设置了一个断点,程序在那里停止,但是当我在该选择器的实际代码中设置断点时,程序不会停止。我关注的选择器是toggleCellAtX:Y:
。这是SPPuzzle.m:
#import "SPPuzzle.h"
@implementation SPPuzzle
- (id)init
{
self = [super init];
if (self) {
int x, y;
for (y=0; y<3; y++)
for (x=0; x<3; x++) {
cells[x][y] = FALSE;
}
}
return self;
}
- (BOOL)cellOnAtX:(int)x Y:(int)y {
return cells[x][y];
}
- (void)toggleCellAtX:(int)x Y:(int)y {
cells[x][y] = !cells[x][y]; // Breakpoint here doesn't stop program.
}
@end
这是SPPuzzleView.m:
#import "SPPuzzleView.h"
@implementation SPPuzzleView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
puzzleInstance = [[SPPuzzle alloc] init];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
int x, y;
for (y=0; y<3; y++)
for (x=0; x<3; x++) {
if ([puzzleInstance cellOnAtX:x Y:y]) {
[[NSColor greenColor] set];
} else {
[[NSColor redColor] set];
}
[[NSBezierPath bezierPathWithRect:NSMakeRect(100*x, 100*y, 100, 100)] fill];
[[NSColor blackColor] set];
[[NSBezierPath bezierPathWithRect:NSMakeRect(100*x, 100*y, 100, 100)] stroke];
}
}
- (void)mouseDown:(NSEvent *)theEvent {
int x = (int)[theEvent locationInWindow].x / 100;
int y = (int)[theEvent locationInWindow].y / 100;
[puzzleInstance toggleCellAtX:x Y:y]; // Breakpoint here does stop program.
[self setNeedsDisplay:TRUE];
}
@end
我添加了评论以显示断点的位置。我做错了什么?
顺便说一句,我在调用方法时使用了Step Into,但它只是转到下一行([self setNeedsDisplay:TRUE];
)。
答案 0 :(得分:2)
puzzleInstance
可能是nil
(0x0
,正如您所说:nil
只是内存地址0
的另一个奇特名称) 。如果您从XIB文件中实例化SPPuzzleView
,就会出现这种情况,因为加载机制会调用initWithCoder:
(然后是awakeFromNib
)而不是initWithFrame:
,而是{{1}未初始化。