2010-05-18 3 views
1

Essayer de créer un Skat-jeu, j'ai rencontré le problème suivant:Comportement étrange de NSString dans NSView

isBidding est une indicationg valeur booléenne, le programme est dans un certain état, [bureau sélectionné] est une méthode appelant revenir le joueur actuellement sélectionné, chatStrings se compose des dictionnaires, des chaînes d'économie avec le joueur, qui a tapé, et ce qu'il tapés

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

Ceci est la partie la détermination du contenu de la chaîne, la chaîne est apportée par un [événement caractères] méthode.

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

Le problème se pose est la suivante:

lors de la frappe dans plus de deux lettres, l'affichage affiche NSRectSet {{{471, 574}, {500, 192}}} et retourne plus discription quand j'essaie de l'imprimer.

puis j'obtiens un message EXC_BAD_ACCESS, bien que je ne l'ai pas publié (pour autant que je puisse voir) J'ai également créé la chaîne avec alloc et init, donc je ne peux pas être dans le pool autorelease. J'ai également essayé de regarder le processus quand il change avec le débogueur, mais je n'ai trouvé aucun code responsable.

Comme vous pouvez le voir, je suis toujours un débutant dans Cocoa (et la programmation en général), donc je serais vraiment heureux si quelqu'un pouvait m'aider avec ça.

Répondre

0

Je n'arrive pas à déchiffrer votre code, mais je peux vous dire quelque chose au sujet du retour impair. NSRectSet est un type privé dans le framework Foundation. Tu ne devrais jamais le voir. Son IIRC utilisé en interne pour représenter les rectangles imbriqués tels que ceux des piles de vues.

Vous obtenez un problème de mémoire étrange qui provoque un pointeur de chaîne pointer sur le NSRectSet ou vous avez foiré votre méthode d'imbrication et vous affectez la valeur NSRectSet à une chaîne.

1

Ce code est poussette:

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

stringByAppendingString: renvoie un objet autorelease d. Vous devez retain ou copy si vous voulez rester dans les parages (peut-être en utilisant un copy ing/retain ing propriété comme self.displayString = [displayString stringByAppendingString:string]; et la déclaration de propriété correspondant/synthé.

Alors pour le moment, vous assignez un objet sera désalloué, mais vous y accéderez plus tard en donnant l'erreur