Strange behaviour of NSString in NSView

Posted by Mee on Stack Overflow See other posts from Stack Overflow or by Mee
Published on 2010-05-18T19:58:14Z Indexed on 2010/05/18 20:00 UTC
Read the original article Hit count: 285

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about nsview