尝试创建一个 Skat-Game,我遇到了以下问题:
isBidding 是一个布尔值指示g,程序处于某种状态,[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];
}
如果有问题是这样的:
当输入两个以上的字母时,视图显示 NSRectSet{{{471, 574},{500, 192}}} 并且当我尝试打印时不再返回任何描述。
然后我收到一条 EXC_BAD_ACCESS 消息,虽然我还没有释放它(据我所知)我还用 alloc 和 init 创建了字符串,所以我不能在自动释放池中。当它随着调试器发生变化时,我也尝试观察该过程,但我找不到任何负责任的代码。
正如你所看到的,我仍然是 Cocoa 的初学者(和一般的编程),所以如果有人能帮助我,我会非常高兴。