为什么我的Cocoa选择器实际上没有被调用?

时间:2022-09-06 15:06:44

I'm stumped. I don't have any errors or even warnings. I set a breakpoint at the line that calls it, and the program stops there, but when I set a breakpoint inside the actual code for that selector, the program doesn't stop. The selector I'm concerned with is toggleCellAtX:Y:. Here's SPPuzzle.m:

我很难过。我没有任何错误甚至警告。我在调用它的行上设置了一个断点,程序在那里停止,但是当我在该选择器的实际代码中设置断点时,程序不会停止。我关心的选择器是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

And here's SPPuzzleView.m:

这是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

I added comments to show where the breakpoints are. What am I doing wrong?

我添加了评论以显示断点的位置。我究竟做错了什么?

By the way, I used Step Into on the invocation of the method, but it just goes to the next line ([self setNeedsDisplay:TRUE];).

顺便说一句,我在调用方法时使用了Step Into,但它只是进入下一行([self setNeedsDisplay:TRUE];)。

1 个解决方案

#1


2  

puzzleInstance is probably nil (0x0 in the watch list, as you noted: nil is just another fancy name for the memory address 0). This would be the case if you instantiated your SPPuzzleView from a XIB file, because the loading mechanism calls initWithCoder: (then awakeFromNib) instead of initWithFrame:, leaving your puzzleInstance uninitialized.

puzzleInstance可能是nil(监视列表中的0x0,正如您所说:nil只是内存地址0的另一个奇特名称)。如果您从XIB文件中实例化SPPuzzleView,就会出现这种情况,因为加载机制会调用initWithCoder :(然后是awakeFromNib)而不是initWithFrame :,使您的puzzleInstance保持未初始化状态。

#1


2  

puzzleInstance is probably nil (0x0 in the watch list, as you noted: nil is just another fancy name for the memory address 0). This would be the case if you instantiated your SPPuzzleView from a XIB file, because the loading mechanism calls initWithCoder: (then awakeFromNib) instead of initWithFrame:, leaving your puzzleInstance uninitialized.

puzzleInstance可能是nil(监视列表中的0x0,正如您所说:nil只是内存地址0的另一个奇特名称)。如果您从XIB文件中实例化SPPuzzleView,就会出现这种情况,因为加载机制会调用initWithCoder :(然后是awakeFromNib)而不是initWithFrame :,使您的puzzleInstance保持未初始化状态。