大佬教程收集整理的这篇文章主要介绍了ios – 公式数组,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
> x 5
> x-5
> x / 5
> x * 5
@R_409_9447@用arrayName [i]加载一个公式并给出x的值并得到答案.有没有办法做到这一点,如果是这样的话怎么样?
typedef CGFloat (^MyFunction)(CGFloat); //using a typedef to ease the heavy Syntax of the blocks MyFunction function1 = ^CGFloat(CGFloat X){ return x + 5; }; MyFunction function2 = ^CGFloat(CGFloat X){ return x - 5; }; MyFunction function3 = ^CGFloat(CGFloat X){ return x / 5; }; MyFunction function4 = ^CGFloat(CGFloat X){ return x * 5; }; NSArray *functions = @[function1,function2,function3,function4];
现在您可以访问数组的任何位置,并执行所选的块,如下所示:
MyFunction MyFunction = functions[3]; CGFloat test = MyFunction(5); //test will hold 25,because the SELEcted block is 'x * 5'
显然,你可以为数组的任何索引更改3.当然,您可以声明上述任何其他函数,并将它们添加到数组中.
我测试过,效果很好.希望它有所帮助.
以上是大佬教程为你收集整理的ios – 公式数组全部内容,希望文章能够帮你解决ios – 公式数组所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。