开发者

Strange behaviour of NSString in NSView

开发者 https://www.devze.com 2022-12-30 23:38 出处:网络
Trying to create a Skat-Game, I encountered the following problem: isBidding is a Boolean value indicationg, the program is in a certain state,

Trying to create a Skat-Game, I encountered the following problem:

isBidding is a Boolean value indicationg, the program is in a certain state, [desk selected] is a method calling returning the current selected player, chatStrings consists of dictionaries, saving strings with the player, who typed, and what he typed

- (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;
}

This is the part determining the string's content, the string is contributed by an [event characters] method.

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

The problem if have is this:

when typing in more than two letters the view displays NSRectSet{{{471, 574},{500, 192}}} and returns no more discription when I try to print it.

then I get an EXC_BAD_ACCESS message, though I have not released it (as far as I can see) I also created the string with alloc and init, so I cannot be in the autorelease pool. I also tried to watch the process when it changes with the debugger, but I couldn't find any responsible code.

As you can see I am still a beginner in Cocoa (and programming in general), so I would be really happy if somebody would help me with this.


This code is buggy:

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

stringByAppendingString: returns an autoreleased object. You need to retain or copy it if you want it to stick around (maybe by using a copying/retaining property like self.displayString = [displayString stringByAppendingString:string]; and the matching property declaration/synth.

So at the moment, you are assigning an object which will be deallocated, but you later access it giving the error.


I can't puzzle out your code but I can tell you something about the odd return.

NSRectSet is a private type inside the Foundation framework. You shouldn't ever see it. Its used internally IIRC to represent nested rectangles such as those of stacks of views.

You're either getting an odd memory problem that causes a string pointer to actually point at the NSRectSet or you've screwed up your method nesting and you're assigning the NSRectSet value to a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消