精通Objective-C系列 - 2) 使用结构和枚举

简介: #import // -------------------------------------------------- // constants for the different kinds of shapes and their colors typedef enum {...
#import <Foundation/Foundation.h>

// --------------------------------------------------
// constants for the different kinds of shapes and their colors

typedef enum {
    kCircle,
    kRectangle,
    kOblateSpheroid
} ShapeType;


typedef enum {
    kRedColor,
    kGreenColor,
    kBlueColor
} ShapeColor;


// --------------------------------------------------
// Shape bounding rectangle

typedef struct {
    int x, y, width, height;
} ShapeRect;


// --------------------------------------------------

// The Shape

typedef struct {
    ShapeType type;
    ShapeColor fillColor;
    ShapeRect bounds;
} Shape;


// --------------------------------------------------
// convert from the ShapeColor enum value to a human-readable name

NSString *colorName (ShapeColor colorName)
{
    switch (colorName) {
        case kRedColor:
            return @"red";
            break;
        case kGreenColor:
            return @"green";
            break;
        case kBlueColor:
            return @"blue";
            break;
    }
    
    return @"no clue";
    
} // colorName


void drawCircle (ShapeRect bounds, ShapeColor fillColor)
{

    NSLog (@"drawing a circle at (%d %d %d %d) in %@",
           bounds.x, bounds.y, 
           bounds.width, bounds.height,
           colorName(fillColor));
    
} // drawCircle


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


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 shapes

void drawShapes (Shape shapes[], int count)
{
    for (int 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 kOblateSpheroid:
                drawEgg (shapes[i].bounds, 
                         shapes[i].fillColor);
                break;
        }
    }
    
} // drawShapes


// --------------------------------------------------
// The main function.  Make the shapes and draw them

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;
    
    ShapeRect rect1 = { 30, 40, 50, 60 };
    shapes[1].type = kRectangle;
    shapes[1].fillColor = kGreenColor;
    shapes[1].bounds = rect1;
    
    ShapeRect rect2 = { 15, 18, 37, 29 };
    shapes[2].type = kOblateSpheroid;
    shapes[2].fillColor = kBlueColor;
    shapes[2].bounds = rect2;
    
    drawShapes (shapes, 3);
    
    return (0);
    
}


运行结果:

drawing a circle at (0 0 10 30) in red
drawing a rectangle at (30 40 50 60) in green
drawing an egg at (15 18 37 29) in blue
drawing a triangle at (47 32 80 50) in red

 

目录
相关文章
|
JSON 开发工具 iOS开发
Objective-C使用位运算设计可复选的枚举
Objective-C使用位运算设计可复选的枚举
186 0
Objective-C使用位运算设计可复选的枚举
|
JSON 开发工具 iOS开发
Objective-C使用位运算设计可复选的枚举
 在软件开发中,枚举是我们会经常会用到的一种编程方式,通过枚举,可以使我们的代码更具可读性与统一性。通常情况下,我们会通过typedef来定义一种枚举的类型来使用
260 0
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
991 2
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
iOS Objective-C 应用连接Azure Storage时,若不关闭账号的匿名访问,程序能正常运行。但关闭匿名访问后,上传到容器时会出现错误:“Public access is not permitted”。解决方法是将创建容器时的公共访问类型从`AZSContainerPublicAccessTypeContainer`改为`AZSContainerPublicAccessTypeOff`,以确保通过授权请求访问。
222 0
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
|
缓存 开发工具 iOS开发
优化iOS中Objective-C代码调起支付流程的速度
优化iOS中Objective-C代码调起支付流程的速度
281 2
|
安全 JavaScript 前端开发
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
831 0
|
iOS开发 容器
iOS 代码规范格式 Objective-C(上)
iOS 代码规范格式 Objective-C
614 0
iOS 代码规范格式 Objective-C(上)
|
编译器 API iOS开发
iOS 代码规范格式 Objective-C(下)
iOS 代码规范格式 Objective-C(下)
332 0