Objective-C code file organization

Source: Internet
Author: User

In the previous article, allCodeThey are all placed in the same file main. H. This is obviously not a good method. When the code is too large, it will be too big. You can split it:

1,Put all enumeration and structure definitions in one file shapedef. h.

 
// Define the typery type enumeration typedef Enum {kcircle, krectangle, koblatesshperoid} shapetype; // define the ry color enumeration typedef Enum {kredcolor, kgreencolor, kbluecolor} shapecolor; // define the ry rectangle area structure typedef struct {int X, Y, width, height;} shaperect; // define the typery structure typedef struct {shapetype type; shapecolor fillcolor; shaperect bounds;} shape;

2,Place all the methods in the shapemethod. h file.

# Import "shapedef. H "// convert" color enumeration "to a string nsstring * colorname (shapecolor colorname) {Switch (colorname) {Case kredcolor: Return @" red "; break; Case kgreencolor: return @ "green"; break; Case kbluecolor: Return @ "blue"; default: Return @ "no clue"; break ;}// draw the void drawcircle (shaperect bounds, shapecolor fillcolor) {nslog (@ "drawing a circle at (% d, % d) in % @", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // drawcircle // draw a rectangle void drawrectangle (shaperect bounds, shapecolor fillcolor) {nslog (@ "drawing a rectangle at (% d, % d, % d, % d) in % @ ", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // drawrectangle // draw an oval void drawegg (shaperect bounds, shapecolor fillcolor) {nslog (@ "drawing an egg at (% d, % d, % d, % d) in % @ ", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // drawegg // draw the geometric shape void drawshapes (shape shapes [], int count) {int I; for (I = 0; I <count; I ++) {Switch (SHAPES [I]. type) {Case kcircle: drawcircle (SHAPES [I]. bounds, shapes [I]. fillcolor); break; Case krectangle: drawrectangle (SHAPES [I]. bounds, shapes [I]. fillcolor); break; Case koblatesshperoid: drawegg (SHAPES [I]. bounds, shapes [I]. fillcolor); break; default: break; }}// drawshapes

3,Finally, use shapemethod. h In the main file main. M.

 
# Import "shapemethod. H "int main () {shape shapes [3]; shaperect rect0 = {0, 0, 10, 30}; shapes [0]. type = kcircle; shapes [0]. fillcolor = kredcolor; shapes [0]. bounds = rect0; shaperect rect1 = {30,40, 50,60}; shapes [1]. type = krectangle; shapes [1]. fillcolor = kgreencolor; shapes [1]. bounds = rect1; shaperect rect2 = {15,18, 37,39}; shapes [2]. type = koblatesshperoid; shapes [2]. fillcolor = kbluecolor; shapes [2]. bounds = rect2; drawshapes (shapes, 3); Return 0 ;}

Note: The three files shapedef. H, shapemethod. H, and Main. m are all in the same directory of the same project.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.