Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 788203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:19:29+00:00 2026-05-14T21:19:29+00:00

I’m new to Objective C. I am trying to define Constants which have behavior

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:19:29+00:00Added an answer on May 14, 2026 at 9:19 pm

    Short version

    Store a geometric representation of your direction (using a vector or a simple angle) and compute the direction names dynamically. To get left and right directions, copy and rotate your vector by ±½π or ±90°, creating a new direction and returning that. If you only have 90° turns, you can use a point { 0, 1 }, and switch the directions, optionally negating them as you do so. ({ 0, 1 } is North; { 1, 0 } is East, { -1, -0 } is West, { -0, -1 } is South, so if you use this approach you’ll have to have some extra code to determine when to negate, and when not to.)

    Long version

    The issue is that variables outside of functions are initialized at compile time. Naturally, this means that you cannot call functions to initialize them. To do something like this, you need to do the following (although you shouldn’t, see below):

    // Direction.h
    
    extern Direction *north;
    
    // Direction.m
    
    Direction *north = nil;
    
    + (void)initialize {
      north = [[Direction alloc] initWithDisplayName:@"North"];
    }
    

    This is, however, an almost entirely terrible way of going about things. What you want to do is as follows:

    // Direction.h
    
    
    @interface Direction (ConstantDirections)
    
    + (Direction *)north;
    // etc...
    
    @end
    
    // Direction.m
    
    + (Direction *)north {
      static Direction *northDirection = nil;
      if (!northDirection) {
        // assume that ConstantDirection is a subclass of
        // Direction that prevents releases etc.
        northDirection = [[ConstantDirection alloc] initWithDisplayName:@"North"];
      }
      return northDirection.
    }
    

    This will prevent other users of the direction from doing evil things (mostly).

    PS: North is a really weird word, especially noticeable when you type it a lot. 😛

    Edit: Some clarification on the use of cardinal directions as separate objects.

    In practice, your direction class will probably look something like this:

    @interface Direction : NSObject <NSCopying,NSCoding> {
      NSString *displayName;
    
      NSPoint offset;
      // XOR
      NSInteger northOffset;
      NSInteger eastOffset;
      // XOR
      CGFloat theta;
    }
    
    @property (copy) NSString *displayName;
    @property (readonly) NSPoint offset;
    
    + (Direction *)north;
    + (Direction *)east;
    + (Direction *)west;
    + (Direction *)south;
    
    - (id)initWithOffset:(NSPoint)offset;
    
    @end
    

    For the purpose of this discussion, we’ll assume the first.

    In this case, your Direction.m file would contain, among other things, the following.

    @implementation Direction 
    
    + (Direction *)north {
      static Direction *northDirection = nil;
      if (!northDirection) {
        // assume that ConstantDirection is a subclass of
        // Direction that prevents releases etc.
        northDirection = [[ConstantDirection alloc] initWithOffset:NSMakePoint(0,DIRECTION_LARGE_OFFSET)];
        northDirection.displayName = @"North";
      }
      return northDirection.
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.