1

Skat-Game を作成しようとすると、次の問題が発生しました。

isBidding はブール値を示します。プログラムは特定の状態にあります。[desk selected] は現在選択されているプレイヤーを返すメソッド呼び出しです。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];
 }

問題がある場合はこれです:

3 文字以上入力すると、ビューに NSRectSet{{{471, 574},{500, 192}}} が表示され、印刷しようとすると説明が返されません。

その後、EXC_BAD_ACCESS メッセージが表示されますが、解放していませんが (私が確認できる限り)、alloc と init で文字列も作成したため、自動解放プールに参加できません。デバッガーで変化するときのプロセスも監視しようとしましたが、責任あるコードは見つかりませんでした。

ご覧のとおり、私はまだ Cocoa (およびプログラミング全般) の初心者なので、誰かがこれを手伝ってくれたら本当にうれしいです。

4

2 に答える 2

1

このコードはバグがあります:

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

stringByAppendingString:autoreleasedオブジェクトを返します。そのままにする必要がある場合は、retainまたはcopyそれを維持する必要があります(おそらく、のようなcopying/ retainingプロパティself.displayString = [displayString stringByAppendingString:string];と一致するプロパティdeclaration/synthを使用します。

そのため、現時点では、割り当てが解除されるオブジェクトを割り当てていますが、後でアクセスするとエラーが発生します。

于 2011-01-18T00:49:54.533 に答える
0

私はあなたのコードを困惑させることはできませんが、奇妙なリターンについて何かをあなたに話すことができます。

NSRectSetは、Foundationフレームワーク内のプライベートタイプです。あなたはそれを見るべきではありません。ビューのスタックなどのネストされた長方形を表すために、内部でIIRCを使用します。

文字列ポインタが実際にNSRectSetを指すという奇妙なメモリの問題が発生しているか、メソッドのネストを台無しにしてNSRectSet値を文字列に割り当てています。

于 2010-05-18T22:13:01.923 に答える