1
votes

I've looked at this over and over again and I can't see the problem. Its probably obvious and I'm probably being an idiot and I apologize in advance for this.

In my interface I have:

@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 get errors is for 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's also an "assignment from incompatible pointer type" warning...but its secondary
            break;
        case 4:
            return "@Square";
            break;

        default:
            break;
    }
}

The thing that drives me batty is that the class methods works just fine:

+ (float)getAngleInDegrees:(PolygonShape *) polyshape;
+ (float)getAngleInRadians:(PolygonShape *) polyshape;
3
init is an instance method, not a class method. You need to create an instance by sending alloc to the class, then send init to that new instance. Thus: [[NSString alloc] init]. Also, you don't need the s variable at all, since you're not using it. You can then replace the assignment under case 3 with a return statement (like the one under case 4).Peter Hosey
Thanks Peter - you're absolutely right. It might make sense to just use [NSString new] but that seems to be discouraged from what I've read.user102288

3 Answers

4
votes

Your getName: method should return (NSString *), not (NSString). I assume this is the error; if so, then yes, the error message could definitely have been more informative.

In fact, in Objective-C you will never see objects getting passed around without their * behind them, not as return values, not as parameters, not as local variables and not as member variables.

BTW, the warning you mention is because you have a typo, mixing up "@foo" with @"foo". The latter is an Objectice-C string literal, the former is a C string literal whose first character just happens to be @.

3
votes

In addition to the other answers, you're using [NSString init] where you should be using [[NSString alloc] init]. However, that will only allocate an empty string, so you'd probably be better off initializing s to either @"" or nil.

1
votes

I think the error is slightly misleading in this case. In Objective-C, it's generally not possible to pass an object by value (including return values). In this case, you declare the return value as NSString rather than NSString*. The declaration should be:

+ (NSString*)getName:(PolygonShape *) polyshape

not

+ (NSString)getName:(PolygonShape *) polyshape