oc中自定义结构体

来源:互联网 发布:windows 10 周年更新 编辑:程序博客网 时间:2024/06/06 03:22

OC中自定义结构体

1. 方式一

//自定义一个结构体

    struct WSBounds{         CGFloat WSWidth;        CGFloat WSHeight;    };

//自定义类型起别名

 typedef struct WSBounds WSBounds;

//快速创建结构体

    static inline WSBounds WSBoundsMake(CGFloat width, CGFloat height){        WSBounds bounds;        bounds.WSWidth = width;        bounds.WSHeight = height;        return bounds;    }

2. 方式二

//快速创建    typedef struct WSBounds {        CGFloat width;        CGFloat height;       } WSBounds;
//快速创建结构体    static inline     WSBounds WSBoundsMake(CGFloat width, CGFloat height    {         WSBounds bounds;        bounds.width = width;        bounds.height = height;        return bounds;    }

3.关于static inline

苹果官方标准结构体的写法

  /* Points. */    struct CGPoint {        CGFloat x;        CGFloat y;    };    typedef struct CGPoint CGPoint;    /*** Definitions of inline functions. ***/    CG_INLINE CGPoint    CGPointMake(CGFloat x, CGFloat y)    {      CGPoint p; p.x = x; p.y = y; return p;    }    //苹果官方宏定义    #  define CG_INLINE static inline

static 标识此内联联函数只能在本文件中使用,限制了内联函数的作用域。相对于宏来说,static inline具有和宏同样级别的开销,而且还提供了类型安全,没有长度和格式的具体限制。

0 0
原创粉丝点击