I am going through the book by Dalrymple, (Learn objective-C on the Macintosh) and include a snippet of the program ....which draws some of the attributes of shapes to the stdout. The code is almost irrelevant to the question, which is the following.
What is best practice in Obj C.
It seems in this case, both the definitions and the declarations occur before "main". Is this the usual practice?
Secondly, **if** for example, "drawshapes" were to have been placed after "main", would the declaration mimic C ( if this is indeed allowed) ie
void drawShapes (Shape shapes[], int count);
or
void drawShapes (Shape [], int );
Thank you in advance.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#import <Foundation/Foundation.h>
// --------------------------------------------------
// constants for the different kinds of shapes and their colors
typedef enum {
kCircle,
/* snip...more shapes */
} ShapeType;
typedef enum {
kRedColor,
/* snip...more colors */
} ShapeColor;
// --------------------------------------------------
// Shapes and their bounds
typedef struct {
int x, y, width, height;
} ShapeRect;
typedef struct {
ShapeType type;
ShapeColor fillColor;
ShapeRect bounds;
} Shape;
// --------------------------------------------------
// convert from the ShapeColor enum value to a human-readable name
NSString *colorName (ShapeColor color)
{
NSString *colorName;
switch (color) {
case kRedColor:
colorName = @"red";
break;
//snip ...more cases;
}
return (colorName);
} // colorName
// --------------------------------------------------
// Shape-drawing function
void drawCircle (ShapeRect bounds,
ShapeColor fillColor)
{
NSLog (@"drawing a circle at (%d %d %d %d) in %@",
//snip...prints circle attributes
} // drawCircle
void drawRectangle (ShapeRect bounds,
ShapeColor fillColor)
{
// more code
} // drawRectangle
// --------------------------------------------------
// Draw each of the shapes
void drawShapes (Shape shapes[], int count)
{
int i;
for (i = 0; i < count; i++) {
switch (shapes[i].type) {
// snip...
}
}
} // drawShapes;
// --------------------------------------------------
// Main program that runs it all. Create some shapes and print
// them out.
int main (int argc, const char * argv[])
{
Shape shapes[3];
ShapeRect rect0 = { 0, 0, 10, 30 };
shapes[0].type = kCircle;
shapes[0].fillColor = kRedColor;
shapes[0].bounds = rect0;
// snip...fill array with other attributes
drawShapes (shapes, 3);
return (0);
} // main