Search code examples
objective-ccuser-defined-types

Defining constants and Objective-C


I'm writing a board game to teach myself Objective C for iPhone and I have a "Piece" class.

Now I would like to make pieces of different shapes, so I would like to add a field "int shape" to the ".h" file of my Piece class. Let's assume I would like to have 3 shapes: square, triangle and circle.

My question is: what is the nicest way to define these sorts of shapes? And where is it best to define them?

Edit: With nicest here I mean most real world, most common, least code and most performant.

Edit No. 2: I used an enum.

For example: I could make a separate header file "PieceConstants.h" and make an enumeration in there. Or I could add the enumeration in the file "Piece.m" so that I could use them like this [Piece squareShapeType].

Many thanks for your help!


Solution

  • Objective C is an object oriented language. So what you would do is define a separate class for each of the shapes of the game pieces, that inherits from the "Piece" class. Then you would have a draw function implemented in each of the shape classes that draws the different shapes. If you want to read about object oriented programming, here is a good resource.
    http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/OOP_ObjC/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40005149-CH1-SW2