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具有和宏同样级别的开销,而且还提供了类型安全,没有长度和格式的具体限制。
本文介绍了在Objective-C(OC)中如何自定义结构体,包括两种方式,并详细讲解了static inline的使用,强调其在提供类型安全方面的优势,与宏定义相比具有更小的开销和无长度与格式限制。

5339

被折叠的 条评论
为什么被折叠?



