• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Obj C ...good style
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Obj C ...good style


  • Subject: Obj C ...good style
  • From: Michael <email@hidden>
  • Date: Sat, 25 Oct 2008 18:52:07 -0700

I hope the list does not mind some basic questions.

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







 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Obj C ...good style
      • From: Ian Joyner <email@hidden>
    • Re: Obj C ...good style
      • From: Ken Thomases <email@hidden>
  • Prev by Date: Re: Just starting off in obj C
  • Next by Date: Re: Problems adding files to cvs repository at sourceforge.net
  • Previous by thread: Re: Problems adding files to cvs repository at sourceforge.net
  • Next by thread: Re: Obj C ...good style
  • Index(es):
    • Date
    • Thread