Objective C - Constants with behaviour

Posted by Akshay on Stack Overflow See other posts from Stack Overflow or by Akshay
Published on 2010-05-19T09:50:00Z Indexed on 2010/05/19 10:00 UTC
Read the original article Hit count: 341

Filed under:
|

Hi, I'm new to Objective C. I am trying to define Constants which have behavior associated with them. So far I have done -

@interface Direction : NSObject {
     NSString *displayName;
}

-(id)initWithDisplayName:(NSString *)aName;

-(id)left; // returns West when North

-(id)right; // return East when North

@end

@implementation Direction

    -(id)initWithDisplayName:(NSString *)aName
    {
        if(self = [super init])
        {
            displayName = aName;
        }   
        return self;
    }

    -(id)left {};
    -(id)right {};

@end

Direction* North = [Direction initWithDisplayName:@"North"]; // error - initializer element is not constant.

This approach results in the indicated error. Is there a better way of doing this in Objective C.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa