2011-01-17 6 views
2

j'ai un objet CGMutablePathRef appelé chemin, je veux savoir, comment puis-je savoir chemin est nul, cela signifie que je n'ai pas utilisécomment vérifier CGMutablePathRef est nulle ou non

CGPathAddLineToPoint(path, NULL, point.x, point.y); 

ou

CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y); 

pour pousser des points dans le chemin.

Répondre

2

Pour vérifier si un chemin est NULL, utilisez l'opérateur ==/!=.

if (path != NULL) 
    CGPathAddLineToPoint(path, NULL, point.x, point.y); 

Pour vérifier si un chemin ne contient pas de rien, utilisez CGPathIsEmpty.

if (!CGPathIsEmpty(path)) 
    CGPathAddLineToPoint(path, NULL, point.x, point.y); 

Pour vérifier si a été déplacé, utilisez CGPathGetCurrentPoint et comparer avec le point d'un chemin (0, 0).

if (!CGPointEqualToPoint(CGPathGetCurrentPoint(path), CGPointZero)) 
    CGPathAddLineToPoint(path, NULL, point.x, point.y); 

Bien sûr, cela ne peut pas distinguer entre un chemin vraiment vide et un chemin que quelqu'un a appelé CGPathMoveToPoint(path, NULL, 0, 0) là-dessus.

(En fait, la raison pour laquelle vous avez besoin de prendre soin? Il suffit de créer un nouveau chemin si vous voulez un vide.)

0

un CGMutablePathRef est un pointeur, donc tout ce que vous devez faire pour tester c'est:

if (NULL != path) { 
    CGPathAddLineToPoint(path, NULL, point.x, point.y); 
} 
+0

mais il y a plusieurs façons d'écrire cela. beaucoup de gens choisissent aussi 'if (chemin)', 'if (chemin! = NULL)', et 'if (0! = chemin)' - ils produisent tous les mêmes résultats. – justin

Questions connexes