Why is my objective c method giving me "error: object cannot be used as parameter to method",
I have looked at this over and over and I see no problem. This is probably obvious and I am probably an idiot and I apologize in advance for that.
In my interface, I:
@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
The part in the implementation that I am getting errors relates to the getName method:
@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 also an "assignment from incompatible pointer type" warning ... but its secondary
break;
case 4:
return "@Square";
break;
default:
break;
}
}
The thing that turns me on is that the class methods work great:
+ (float)getAngleInDegrees:(PolygonShape *) polyshape;
+ (float)getAngleInRadians:(PolygonShape *) polyshape;
a source to share
Your getName: method should return (NSString *), not (NSString). I am guessing this is a bug; if yes, then yes, the error message could be more informative.
In fact, in Objective-C you will never see objects passed without them * behind them, not as return values, not as parameters, not as local variables, not as member variables.
By the way, the warning you are talking about is because you have a typo by mixing "@foo" with @ "foo". The latter is an Objectice-C string literal, the former is a C string literal whose first character is @.
a source to share
I think the error in this case is a bit misleading. In Objective-C, it is usually not possible to pass an object by value (including return values). In this case, you are specifying the return value as NSString
, not NSString*
. The declaration must be:
+ (NSString*)getName:(PolygonShape *) polyshape
not
+ (NSString)getName:(PolygonShape *) polyshape
a source to share