NSView中NSString的奇怪行为

时间:2010-05-18 19:58:15

标签: cocoa nsview

尝试创建一个Skat-Game,我遇到了以下问题:

isBidding是一个布尔值指示,程序处于某种状态, [桌面选择]是一种调用返回当前所选播放器的方法, chatStrings由字典组成,与播放器保存字符串,键入内容以及键入的内容

- (void)drawRect:(NSRect)rect{

    NSMutableDictionary * attributes = [NSMutableDictionary dictionary];

    [attributes setObject:[NSFont fontWithName:playerFont size:playerFontSize] forKey:NSFontAttributeName];
    [attributes setObject:playerFontColor forKey:NSForegroundColorAttributeName];


    [[NSString stringWithFormat:@"Player %i",[desk selected] + 1] drawInRect:playerStringRect withAttributes:attributes];

    if (isBidding){
        [attributes setObject:[NSFont fontWithName:chatFont size:chatFontSize] forKey:NSFontAttributeName];
        [attributes setObject:chatFontColor forKey:NSForegroundColorAttributeName];

        int i;
        for (i = 0; i < [chatStrings count]; i++, yProgress -= 20){
            if (isBidding)
                [[NSString stringWithFormat:@"Player %i bids: %@",
                    [[[chatStrings objectAtIndex:i]valueForKey:@"Player"]intValue],
                    [[chatStrings objectAtIndex:i]valueForKey:@"String"]],
                        drawAtPoint:NSMakePoint([self bounds].origin.x, yProgress) withAttributes:attributes];   
            else
                [[NSString stringWithFormat:@"Player %i: %@",[[[chatStrings objectAtIndex:i] valueForKey:@"Player"]intValue],
                [[chatStrings objectAtIndex:i]valueForKey:@"String"]]
                    drawAtPoint:NSMakePoint([self bounds].origin.x, yProgress) withAttributes:attributes];

        }   
    }

    if (isBidding)
        [[NSString stringWithFormat:@"Player %i bids: %@",[desk selected] + 1, displayString]
            drawAtPoint:NSMakePoint([self bounds].origin.x, yProgress) withAttributes:attributes];
    else
        [[NSString stringWithFormat:@"Player %i: %@",[desk selected] + 1, displayString]
            drawAtPoint:NSMakePoint([self bounds].origin.x, yProgress) withAttributes:attributes];

    yProgress = chatFontBegin;
}

这是确定字符串内容的部分,字符串由[event characters]方法提供。

-(void)displayChatString:(NSString *)string{
     displayString = [displayString stringByAppendingString:string];
     [self setNeedsDisplay:YES];
 }

问题如果有:

当输入两个以上的字母时,视图会显示NSRectSet {{{471,574},{500,192}}} 当我尝试打印它时,不再返回描述。

然后我得到一条EXC_BAD_ACCESS消息,虽然我还没有发布它(据我所知)我还用alloc和init创建了字符串,所以我不能在自动释放池中。 我还尝试用调试器更改过程,但我找不到任何负责任的代码。

正如你所看到的,我仍然是Cocoa的初学者(以及一般的编程),所以如果有人能帮助我,我会非常高兴。

2 个答案:

答案 0 :(得分:1)

此代码有问题:

-(void)displayChatString:(NSString *)string{
     displayString = [displayString stringByAppendingString:string];
     [self setNeedsDisplay:YES];
 }

stringByAppendingString:返回autorelease d个对象。您需要retaincopy它,如果您希望它(例如copy使用retain / self.displayString = [displayString stringByAppendingString:string];属性{{1}}和匹配财产声明/合成。

所以目前,您正在分配一个将被解除分配的对象,但您稍后会访问它并给出错误。

答案 1 :(得分:0)

我无法解开你的代码,但我可以告诉你一些奇怪的回报。

NSRectSet是Foundation框架内的私有类型。你永远不应该看到它。它在内部使用IIRC来表示嵌套的矩形,例如视图堆栈的矩形。

你要么得到一个奇怪的内存问题,导致字符串指针实际指向NSRectSet,要么你搞砸了你的方法嵌套,并且你将NSRectSet值赋给一个字符串。