Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Short version</strong></p> <p>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.)</p> <p><strong>Long version</strong></p> <p>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):</p> <pre><code>// Direction.h extern Direction *north; // Direction.m Direction *north = nil; + (void)initialize { north = [[Direction alloc] initWithDisplayName:@"North"]; } </code></pre> <p>This is, however, an almost entirely terrible way of going about things. What you <em>want</em> to do is as follows:</p> <pre><code>// 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. } </code></pre> <p>This will prevent other users of the direction from doing evil things (mostly).</p> <p><strong>PS:</strong> North is a really weird word, especially noticeable when you type it a lot. :P</p> <p><strong>Edit:</strong> Some clarification on the use of cardinal directions as separate objects.</p> <p>In practice, your direction class will probably look something like this:</p> <pre><code>@interface Direction : NSObject &lt;NSCopying,NSCoding&gt; { 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 </code></pre> <p>For the purpose of this discussion, we'll assume the first.</p> <p>In this case, your Direction.m file would contain, among other things, the following.</p> <pre><code>@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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload