xcode 的布尔函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12564529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:36:25  来源:igfitidea点击:

Bool function for xcode

objective-cxcode

提问by Hyman Nick

I am practicing some bool functions and I seem to be stuck any help will be appreciated. I must be making some little mistake.

我正在练习一些布尔函数,但我似乎被困住了,任何帮助将不胜感激。我一定是犯了一些小错误。

-(BOOL) checkForWin
{
    if ([[dictionary valueForKey:[cowsShuffled objectAtIndex:cowsCard]] intValue]  == 2{
       return YES;
    }
}

 -(void) moo 
{
    if (checkForWin == YES) {
        NSLog (@"foo");
    }
}

回答by Marcelo Cantos

You need to call the method (not function), and you don't need to compare to YES. The ifstatement does that implicitly:

您需要调用方法(而不是函数),并且不需要与 YES 进行比较。该if语句隐含地做到了这一点:

if ([self checkForWin]) …

Also note that checkForWinhas a problem: it doesn't return anything if the ifstatement fails. It should be simply:

另请注意,checkForWin有一个问题:如果if语句失败,它不会返回任何内容。它应该是简单的:

- (BOOL)checkForWin{
    return [[dictionary valueForKey:[cowsShuffled objectAtIndex:cowsCard]] intValue] == 2;
}

Footnote:Strictly speaking, if (x) …isn't exactly the same as if (x == YES) …. It's actually closer to if (x != NO) …, but of course that's the same thing for most intents and purposes (and those for which it isn't are largely pathological).

脚注:严格来说,if (x) …if (x == YES) …. 它实际上更接近于if (x != NO) …,但当然,对于大多数意图和目的(以及那些不是主要是病态的)来说,这当然是一样的。

回答by DrummerB

Your method call is wrong. You call a method like this: [object method].

你的方法调用是错误的。你叫这样的方法:[object method]

In your case [self checkForWin].

在你的情况下[self checkForWin]