iOS可变参数宏定义的Demo使用以及分析

前言:近期看了一下RAC中的宏命令,发现其中的宏命令一环扣一环,环环相扣,把我都给整懵逼了...


iOS可变参数宏定义的Demo使用以及分析_第1张图片


通过深入学习,终于能看懂了,写个小demo秀一波操作.



源码:

#define JK_Prepare_Subviews(supperView, ...)\

JK_metamacro_foreach(JK_Add_Subview, supperView, __VA_ARGS__)

#define JK_metamacro_foreach(Marco, supperView, ...)\

JK_metamacro_concat(JK_metamacro_foreach, JK_metamacro_argcount(__VA_ARGS__))(Marco, supperView, __VA_ARGS__)

#define JK_metamacro_foreach20(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19)\

JK_metamacro_foreach19(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18)\

Marco(Parameter, _19, 19)

#define JK_metamacro_foreach19(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18)\

JK_metamacro_foreach18(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17)\

Marco(Parameter, _18, 18)

#define JK_metamacro_foreach18(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17)\

JK_metamacro_foreach17(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16)\

Marco(Parameter, _17, 17)

#define JK_metamacro_foreach17(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16)\

JK_metamacro_foreach16(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15)\

Marco(Parameter, _16, 16)

#define JK_metamacro_foreach16(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15)\

JK_metamacro_foreach15(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14)\

Marco(Parameter, _15, 15)

#define JK_metamacro_foreach15(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14)\

JK_metamacro_foreach14(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13)\

Marco(Parameter, _14, 14)

#define JK_metamacro_foreach14(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13)\

JK_metamacro_foreach13(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12)\

Marco(Parameter, _13, 13)

#define JK_metamacro_foreach13(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12)\

JK_metamacro_foreach12(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11)\

Marco(Parameter, _12, 12)

#define JK_metamacro_foreach12(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11)\

JK_metamacro_foreach11(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10)\

Marco(Parameter, _11, 11)

#define JK_metamacro_foreach11(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10)\

JK_metamacro_foreach10(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9)\

Marco(Parameter, _10, 10)

#define JK_metamacro_foreach10(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9)\

JK_metamacro_foreach9(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8)\

Marco(Parameter, _9, 9)

#define JK_metamacro_foreach9(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7, _8)\

JK_metamacro_foreach8(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7)\

Marco(Parameter, _8, 8)

#define JK_metamacro_foreach8(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6, _7)\

JK_metamacro_foreach7(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6)\

Marco(Parameter, _7, 7)

#define JK_metamacro_foreach7(Marco, Parameter, _0, _1, _2, _3, _4, _5, _6)\

JK_metamacro_foreach6(Marco, Parameter, _0, _1, _2, _3, _4, _5)\

Marco(Parameter, _6, 6)

#define JK_metamacro_foreach6(Marco, Parameter, _0, _1, _2, _3, _4, _5)\

JK_metamacro_foreach5(Marco, Parameter, _0, _1, _2, _3, _4)\

Marco(Parameter, _5, 5)

#define JK_metamacro_foreach5(Marco, Parameter, _0, _1, _2, _3, _4)\

JK_metamacro_foreach4(Marco, Parameter, _0, _1, _2, _3)\

Marco(Parameter, _4, 4)

#define JK_metamacro_foreach4(Marco, Parameter, _0, _1, _2, _3)\

JK_metamacro_foreach3(Marco, Parameter, _0, _1, _2)\

Marco(Parameter, _3, 3)

#define JK_metamacro_foreach3(Marco, Parameter, _0, _1, _2)\

JK_metamacro_foreach2(Marco, Parameter, _0, _1)\

Marco(Parameter, _2, 2)

#define JK_metamacro_foreach2(Marco, Parameter, _0, _1)\

JK_metamacro_foreach1(Marco, Parameter, _0)\

Marco(Parameter, _1, 1)

#define JK_metamacro_foreach1(Marco, Parameter, _0)\

Marco(Parameter, _0, 0)

#define JK_Add_Subview(supperView, suberView, Index)\

[supperView addSubview:suberView];

#define JK_metamacro_concat(A, B) \

JK_metamacro_concat_(A, B)

#define JK_metamacro_concat_(A, B) A ## B

#define JK_metamacro_argcount(...) \

JK_metamacro_at(20, __VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

#define JK_metamacro_at(N, ...) \

JK_metamacro_concat_(JK_metamacro_at, 20)(__VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

#define JK_metamacro_at20(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, ...) JK_metamacro_head(__VA_ARGS__)

#define JK_metamacro_head(...) \

JK_metamacro_head_(__VA_ARGS__, 0)

//得到结果: 参数个数

#define JK_metamacro_head_(FIRST, ...) FIRST



试用一下:

- (void)viewDidLoad {

[super viewDidLoad];

NSMutableArray *marr = [NSMutableArray array];

CGFloat margin = 5;

CGFloat y;

CGFloat height = 20;

for (int i = 0; i < 21; i++) {

y = height * i + margin * (i + 1);

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, y, 375, height)];

label.text = [NSString stringWithFormat:@"第%d行",i];

label.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];

[marr addObject:label];

}

JK_Prepare_Subviews(self.view, marr[0], marr[1], marr[2], marr[3], marr[4], marr[5] ,marr[6], marr[7], marr[8], marr[9], marr[10], marr[11], marr[12], marr[13], marr[14], marr[15], marr[16], marr[17], marr[18], marr[19]);

}


iOS可变参数宏定义的Demo使用以及分析_第2张图片
效果图

是不是看不懂?


iOS可变参数宏定义的Demo使用以及分析_第3张图片

实例解析:传入三个参数:a, b, c

JK_Prepare_Subviews(supperView, a, b, c)

#define JK_Prepare_Subviews(supperView, ...)\

JK_metamacro_foreach(JK_metamacro_foreach, supperView, __VA_ARGS__)

第一次展开

JK_metamacro_foreach(JK_metamacro_foreach, supperView, a, b, c)



#define JK_metamacro_foreach(Marco, supperView, ...)\

JK_metamacro_concat(JK_metamacro_foreach, JK_metamacro_argcount(__VA_ARGS__))(Marco, supperView, __VA_ARGS__)

第二次展开

JK_metamacro_concat(JK_metamacro_foreach, JK_metamacro_argcount(a, b, c))(JK_Add_Subview, supperView, a, b, c)

参数中的宏展开

#define JK_metamacro_argcount(...) \

JK_metamacro_at(20, a, b, c, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

#define JK_metamacro_at(N, ...) \

JK_metamacro_concat_(JK_metamacro_at, 20)(a, b, c, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

截取之后就剩下了3, 2, 1  {_0 ~ _19这20个参数(a, b, c, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4)在这边没有传递到下一个宏中}

#define JK_metamacro_at20(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, ...) JK_metamacro_head(3, 2, 1)

#define JK_metamacro_head(...) \

JK_metamacro_head_(3, 2, 1, 0)

得到结果: 参数个数

#define JK_metamacro_head_(FIRST, ...) 3



#define JK_metamacro_concat(A, B) \

JK_metamacro_concat_(A, B)

第三次展开

JK_metamacro_concat_(JK_metamacro_foreach, 3)(JK_Add_Subview, supperView, a, b, c)



#define JK_metamacro_concat_(A, B) A ## B      //拼接宏,结果为AB

第四次展开

JK_metamacro_foreach3(JK_Add_Subview, supperView, a, b, c)

#define JK_metamacro_foreach3(Marco, Parameter, _0, _1, _2)\

JK_metamacro_foreach2(Marco, Parameter, _0, _1)\

Marco(Parameter, _2, 2)



第五次展开

JK_metamacro_foreach2(JK_Add_Subview, supperView, a, b)

JK_Add_Subview(supperView, c)

#define JK_metamacro_foreach2(Marco, Parameter, _0, _1)\

JK_metamacro_foreach1(Marco, Parameter, _0)\

Marco(Parameter, _1, 1)



第六次展开

JK_metamacro_foreach1(JK_Add_Subview, supperView, a)

JK_Add_Subview(supperView, b)

JK_Add_Subview(supperView, c)

第七次展开

JK_Add_Subview(supperView, a)

JK_Add_Subview(supperView, b)

JK_Add_Subview(supperView, c)



结果:

宏定义其实就是是替换,

JK_Prepare_Subviews(supperView, a, b, c)

层层展开,最后替换之后就变成了

JK_Add_Subview(supperView, a)

JK_Add_Subview(supperView, b)

JK_Add_Subview(supperView, c)

最后附上Demo地址:戳我

喜欢请给个赞哦~

你可能感兴趣的:(iOS可变参数宏定义的Demo使用以及分析)