J'ai regardé cela encore et encore et je ne peux pas voir le problème. C'est probablement évident et je suis probablement un idiot et je m'excuse d'avance pour cela.pourquoi ma méthode objective c me donne une "erreur: ne peut pas utiliser un objet comme paramètre à une méthode"
Dans mon interface je:
@interface PolygonShape : NSObject { int numberOfSides; int minimumNumberOfSides; int maximumNumberOfSides; } @property int numberOfSides, minimumNumberOfSides, maximumNumberOfSides; // class methods + (float)getAngleInDegrees:(PolygonShape *) polyshape; + (float)getAngleInRadians:(PolygonShape *) polyshape; + (NSString)getName:(PolygonShape *) polyshape; //instance methods - (id)init; - (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max; @end
La partie dans la mise en œuvre que je reçois des erreurs est la méthode getName:
@implentation... + (NSString)getName:(PolygonShape *) polyshape { // here is where I get the "error: can not use an object as parameter to a method" int sides = [polyshape numberOfSides]; NSString * s = [NSString init]; switch (sides) { case 3: s = "@Triangle"; // there's also an "assignment from incompatible pointer type" warning...but its secondary break; case 4: return "@Square"; break; default: break; } }
Ce qui me pousse Batty est que les méthodes de classe fonctionne très bien:
+ (float)getAngleInDegrees:(PolygonShape *) polyshape;
+ (float)getAngleInRadians:(PolygonShape *) polyshape;
init est une méthode d'instance, pas une méthode de classe. Vous devez créer une instance en envoyant alloc à la classe, puis envoyer init à cette nouvelle instance. Ainsi: [[NSString alloc] init]. De plus, vous n'avez pas du tout besoin de la variable s, puisque vous ne l'utilisez pas. Vous pouvez ensuite remplacer l'affectation dans le cas 3 par une déclaration de retour (comme celle du cas 4). –
Merci Peter - vous avez absolument raison. Il peut être judicieux d'utiliser simplement [NSString new] mais cela semble découragé par rapport à ce que j'ai lu. – user102288