tags:

views:

411

answers:

3

Still new to Objective C, and I'm having some trouble that I just can't seem to figure out on my own. The error occurs twice for each of the first three @properties below, and is: error: "syntax error before ')' token".

#import <Foundation/Foundation.h>    

@interface PolygonShape : NSObject {
    int *numberOfSides;
    int *minimumNumberOfSides;
    int *maximumNumberOfSides;
}

@property (setter = setNumberOfSides) int *numberOfSides;
@property (setter = setMin) int *minimumNumberOfSides;
@property (setter = setMax) int *maximumNumberOfSides;
@property (readonly, getter = angleInDegrees) float *angleInDegrees;
@property (readonly, getter = angleInRadians) float *angleInRadians;
@property (readonly, getter = name) NSString *name;

- (id) init;
- (id) initWithSides: (int*) sides  min: (int*) min  max: (int*) max;
- (void) dealloc;
- (BOOL) setNumberOfSides: (int*) num;
- (void) setMin: (int*) num;
- (void) setMax: (int*) num;
- (void) description;
- (float*) angleInDegrees;
- (float*) angleInRadians;
- (NSString*) name;

@end

Not sure if this would make a difference, but here is a summarized version of the implementation:

#import "PolygonShape.h";

@implementation PolygonShape

@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;

...

- (BOOL) setNumberOfSides: (int*) num {
    if(num > minimumNumberOfSides && num < maximumNumberOfSides) {
     [numberOfSides release];
     numberOfSides = [num retain];
     return YES;
    } else {
     NSLog(@"Number %i is out of range %i to %i", num, minimumNumberOfSides, maximumNumberOfSides);
     return NO;
    }
}

- (BOOL) setMin: (int*) num {
    if(num > 2 && num <= maximumNumberOfSides) {
     [minimumNumberOfSides release];
     minimumNumberOfSides = [num retain];
     return YES;
    } elseif (num > 2 && maximumNumberOfSides == nil) {
     [minimumNumberOfSides release];
     [self setMax: 12];
     minimumNumberOfSides = [num retain];
     return YES;
    } elseif (num > 2) {
     NSLog(@"Polygons must have more than 2 sides.");
     return NO;
    } else {
     NSLog(@"Please enter a number less than or equal to %i, the current maximum", maximumNumberOfSides);
     return NO;
    }
}

- (void) setMax: (int*) num {
    if(num <= 12 && num < maximumNumberOfSides) {
     [maximumNumberOfSides release];
     maximumNumberOfSides = [num retain];
    } elseif (num <= 12) {
     NSLog(@"Please enter a number less than or equal to 12");
    } else {
     NSLog(@"Please enter a number greater than or equal to %i, the current minimum", minimumNumberOfSides);
    }
}

...

@end

Thanks in advance for your help!

+3  A: 

I see a few things that need to be cleaned up here, but the most obvious mistake is that you're treating primitive types, such as int and float, as pointers (you should also be using NSInteger and CGFloat, but that's not as critical). You're going to need to change a lot of code, so fix this before worrying about anything else. Here's a C tutorial that might help you learn the distinction between primitive types and pointers.

Marc Charbonneau
Thank you for your help. My idea of how Cocoa uses objects was very skewed. Reading some C tutorials and asking a couple more questions regarding Obj C objects helped everything click.
Bloudermilk
+1  A: 

You're using the wrong selectors on your setter properties. The full selector name includes the colon at the end: setNumberOfSides:

They should look like this:

@interface Foo
@property (readwrite, setter = setNumberOfSides:) int numberOfSides;
@end

One thing though, the names you're specifying are the same as the automatically generated names so you don't need to specify the setter attribute.

Ashley Clark
Wonderful! Once I got all my pointers straightened out this was the only thing left to fix and you made it seem so obvious. Thanks so much.
Bloudermilk
+3  A: 

A few more things about this code:

  • You have a semicolon ; after the #import directive. That's a preprocessor directive, not a statement, and thus doesn't need (and will never have) a terminating semicolon.

  • You're writing elseif instead of else if. There is no elseif keyword in Objective-C, there are only the if keyword and the else keyword.

  • You're both declaring a property (which declares a setter) and the setter method. Objective-C doesn't need a declaration in the interface of a class for every method the class implements — and when using properties, the property declaration is the setter method declaration.

  • You have a setter that also has a return value. Don't do that.

  • You're using a different coding style than the Cocoa headers and examples.

In general, when writing Objective-C code, you should try to write in the same style as the framework headers and examples. For example, you're putting whitespace between method parameter/return types and parts of the method name; this isn't the usual coding style in Cocoa. Getting used to the usual coding style will make it easier for other developers to pick up your own code, and make it easier for you to learn from others' code.

Chris Hanson