Obj C - accidental @property error

Still new to Objective C and am having problems that I just cannot figure out on my own. The error occurs twice for each of the first three @properties below, and this 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 will make a difference, but here is a short 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!

+1


source to share


3 answers


You are using the wrong selectors for your setter properties. The fully qualified selector name includes a colon at the end:setNumberOfSides:

They should look like this:



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

      

One thing however, the names you provide are the same as the auto-generated names, so you don't need to specify the setter attribute.

+1


source


I see a few things to clear up here, but the most obvious mistake is that you treat primitive types like int and float as pointers (you should also use NSInteger and CGFloat, but this is not so critical). You will need to change a lot of code, so fix that before worrying about anything else. Here's a C tutorial that can help you learn the difference between primitive types and pointers.



+3


source


A few things about this code:

  • You have a semicolon ;

    after the directive #import

    . This is a preprocessor directive, not an instruction, and therefore does not need (and never will) a terminating semicolon.

  • You write elseif

    instead else if

    . There is elseif

    no keyword in Objective-C , only a keyword if

    and a keyword else

    .

  • You declare a property (which declares a setter) and a setter method. Objective-C does not require a declaration in the class interface for every method that 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. Do not do that.

  • You are using a different coding style than Cocoa headers and examples.

In general, when writing Objective-C code, you should try to write in the same style as headers and sample frames. For example, you put spaces between the types of the method parameters / return types and the parts of the method name; this is not the usual coding style in Cocoa. Getting used to common coding style will make it easier for other developers to pick their own code and make it easier for you to learn other users' code.

+3


source







All Articles