0

所以,我试图有一种有效地做到这一点的方法:

- (void)doWhile: (/*some magical type*/)condition
{
    while (condition)
    {
        // do some magical things
    }
}

虽然您的第一个建议可能是BOOL考虑以下例外情况:

[someObject doWhile: someOtherObject];
// yes, I know that I could just do (someOtherObject != nil), but
// I should be able to just use (someOtherObject), right?
// seeing as how ifs/fors/whiles can use just the object.

[someObject doWhile: [someOtherObject isValid]];
// since -isValid returns a BOOL, this will work, but it will only
// pass the value of -isValid at the time of calling to the while loop.
// if the value of -isValid changes, -doWhile: will have no idea of the change,
// whereas while() would.

原语的使用_Bool使我能够解决前一个问题,但后一个问题仍然存在。是否有某种方法可以评估与类型无关的参数的真实性与while()工作方式相同?

4

1 回答 1

0

正如评论中所指出的,传递一个块是获得所需结果的一种通用方法,即使更简单的方法可能适用于不需要完全动态评估的测试用例。

于 2012-06-03T18:15:19.010 回答