添加"forCount"的最佳方法控制结构到Objective-C? [英] Best way to add a "forCount" control structure to Objective-C?

查看:41
本文介绍了添加"forCount"的最佳方法控制结构到Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亚当·柯(Adam Ko),亚当·柯(Adam Ko)为这个问题提供了一个宏伟的解决方案.

Adam Ko has provided a magnificent solution to this question, thanks Adam Ko.

顺便说一句,如果像我一样,您喜欢c预处理程序(处理#defines的事物),那么您可能不知道XCode中有一件方便的事情:右键单击您的一个开源文件的正文,转到在底部附近.. 预处理" .它实际上运行预处理器,向您显示总体真实交易".将要编译的内容.太好了!

BTW if, like me, you love the c preprocessor (the thing that handles #defines), you may not be aware there is a handy thing in XCode: right click on the body of one of your open source files, go down near the bottom .. "Preprocess". It actually runs the preprocessor, showing you the overall "real deal" of what is going to be compiled. It's great!

这个问题是样式和代码清晰度的问题.认为它类似于关于微妙的命名问题的问题,或者是在可用习语中最佳选择(更具可读性,更易维护)的问题.

This question is a matter of style and code clarity. Consider it similar to questions about subtle naming issues, or the best choice (more readable, more maintainable) among available idioms.

当然,人们使用这样的循环:

As a matter of course, one uses loops like this:

for(NSUInteger _i=0; _i<20; ++_i)
  {
  .. do this 20 times ..
  }

要清楚,效果是做某事N次.(您 使用正文中的索引.)

To be clear, the effect is to to do something N times. (You are not using the index in the body.)

我想向读者清楚地表明这是一个基于计数的循环-即,索引无关紧要,并且算法上我们正在做N次.

I want to signal clearly for the reader that this is a count-based loop -- ie, the index is irrelevant and algorithmically we are doing something N times.

因此,我想以一种干净的方式做N次身体,没有皇帝的纠缠或浪漫的承诺.您可以创建这样的宏:

Hence I want a clean way to do a body N times, with no imperial entanglements or romantic commitments. You could make a macro like this:

 #define forCount(N) for(NSUinteger __neverused=0; __neverused<N; ++__neverused)

,并且有效.因此,

forCount(20)
  {
  .. do this 20 times ..
  }

然而,可以想到的是,隐藏"如果将来与某些内容发生冲突,则在那里使用的变量可能会引起麻烦.(也许,如果您嵌套有问题的控件结构,还有其他问题.)

However, conceivably the "hidden" variable used there could cause trouble if it collided with something in the future. (Perhaps if you nested the control structure in question, among other problems.)

要明确效率等,这里不是问题.实际上已经有一些不同的控制结构(while,do等),它们实际上是完全相同的东西,但是它们仅作为样式问题存在,仅用于指示,并清楚地表明读者可以了解相关代码段落的预期算法含义."forCount"这是另一个这样需要的控制结构,因为与索引无关"是指索引无关".计数循环在任何算法编程中都是完全基础的.

To be clear efficiency, etc., is not the issue here. There are already a few different control structures (while, do, etc etc) that are actually of course exactly the same thing, but which exist only as a matter of style and to indicate clearly to the reader the intended algorithmic meaning of the code passage in question. "forCount" is another such needed control structure, because "index-irrelevant" count loops are completely basic in any algorithmic programming.

有人知道这个真的,真的,真的很酷的解决方案吗?提到的#define只是让人不满意,您已经抛出了一个变量名,不可避免地有人会踩到它.

Does anyone know the really, really, REALLY cool solution to this? The #define mentioned is just not satisfying, and you've thrown in a variable name that inevitably someone will step on.

谢谢!

基本上有几个人问但是为什么呢?"

A couple of people have asked essentially "But why do it?"

请看以下两个代码示例:

Look at the following two code examples:

for ( spaceship = 3; spaceship < 8; ++spaceship )
   {
   beginWarpEffectForShip( spaceship )
   }

forCount( 25 )
   {
   addARandomComet
   }

对于读者来说,效果当然是完全不同的.

Of course the effect is utterly and dramatically different for the reader.

毕竟,在c语言中有许多控制结构(完全相同),唯一的区别是样式:也就是说,将内容传达给读者.

After all, there are alresdy numerous (totally identical) control structures in c, where the only difference is style: that is to say, conveying content to the reader.

我们都使用非索引相对"每次触摸键盘时都会循环播放(做5次"),就像饼一样自然.

We all use "non-index-relative" loops ("do something 5 times") every time we touch a keyboard, it's as natural as pie.

因此,#define是一个不错的解决方案,有没有更好的方法呢?干杯

So, the #define is an OKish solution, is there a better way to do it? Cheers

推荐答案

更好的方法是允许嵌套的forCount结构-

A better way is to do this to allow nested forCount structure -

#define $_TOKENPASTE(x,y)   x##y
#define $$TOKENPASTE(x,y)   $_TOKENPASTE(x, y)

#define $itr          $$TOKENPASTE($_itr_,__LINE__)
#define forCount(N)   for (NSUInteger $itr=0; $itr<N; ++$itr)

然后您可以像这样使用它

Then you can use it like this

forCount(5)
{
    forCount(10)
    {
        printf("Hello, World!\n");
    }
}

修改:您在评论中建议的问题可以很容易地解决.只需将上面的宏更改为

The problem you suggested in your comment can be fixed easily. Simply change the above macro to become

#define $_TOKENPASTE(x,y)   x##y
#define $$TOKENPASTE(x,y)   $_TOKENPASTE(x, y)

#define UVAR(var)           $$TOKENPASTE(var,__LINE__)
#define forCount(N)         for (NSUInteger UVAR($itr)=0, UVAR($max)=(NSUInteger)(N); \
                                 UVAR($itr)<UVAR($max); ++UVAR($itr))

它的作用是读取在 forCount 参数中给出的表达式的值,并使用该值进行迭代,这样就避免了多次求值.

What it does is that it reads the value of the expression you give in the parameter of forCount, and use the value to iterate, that way you avoid multiple evaluations.

这篇关于添加"forCount"的最佳方法控制结构到Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆