2-dimensional arrays in Objective-C?

I am working on a basic iPhone game that requires a singleton screen. There is nothing complicated. I come from C-background, so my current solution looks something like this:

typedef struct _Tile {
    NSString *type;
} Tile;

@interface Map {
    Tile mapData[MAP_TILE_MAX_X][MAP_TILE_MAX_Y];
}

This works fine, but I'm wondering if there is a slightly more “correct” way of handling things through Objective-C. Here, as I see the situation, if I should take the Objective-C approach: I would create a base Tile class to store the basic properties of the tile, which I could then subclass for certain types of tiles (for example, t21>). This would also allow me to have Tile-specific logic. For example: the Tile class may have a think method that will execute any necessary logic. In my Water subclass, this may include creating a ripple effect if the player is immersed in water.

My questions:

  • Can C structures be used in this situation? If not, am I on the right track regarding my Obj-C approach?
  • Tile Tile, Tile (, NSString, "", Water).
+5
2

, .

NSClassFromString()

, WaterTile UIView (, -, , )

UIView *newTile = [[NSClassFromString([NSString stringWithFormat:@"%@TileView", [@"water" capitalizedString]]) alloc] init];

.

, - , , OO, UIView, , . .

, Tile, , , , UIView, , Tile @protocol.

@protocol TileViewDrawing 
- (void)drawThinking;
@end

@interface WaterTileView : UIView <TileViewDrawing>
@end

@implementation WaterTileView
-(void)drawThinking
{
    // Code to show rippling effect
}
@end

2D- Map, NSArray () NSArrays ()

+1

- .., , .

Apple , NSRect NSPoint NSSize.

, :

typedef enum{
  TileTypeDefault = 0,
  TileTypeSomething,
  TileTypeLOL
} TileType;

((TileType) ) { case TileTypeDefault:    [[TileDefault alloc] initWithSize: size];    break; }

.., , : P

-1

All Articles