If语句可以告诉作业是否有效?

时间:2022-01-18 22:23:45

I have an object that returns a value if successful and false (or nil) if it failed.

我有一个对象,如果成功则返回一个值,如果失败则返回false(或nil)。

i want to assign that value to a variable

我想将该值赋给变量

if(var1 = [object foo])
{
    //if the [object foo] returned a variable, goes here
}
else
{
    //[object foo] returned FALSE (or nil), go here
}

can an If statement detected if an assignment was valid?

如果作业有效,可以检测到If语句吗?

4 个解决方案

#1


2  

Not sure I understand your question, but let me try and explain a few situations you can check

我不确定我理解你的问题,但让我试着解释你可以检查的一些情况

1) Property contains value

1)财产包含价值

if ([object foo])
{
    // If foo has a value associated to it that is not nil/false/zero
}
else
{
    // If foo equals nil, false or zero
}

2) Assignment to a variable was successful

2)对变量的赋值是成功的

if ((bar = [object myMethod]))
{
    // If myMethod returns any non-nil value
}
else
{
    // If myMethod returns nil
}

3) Previous assignment of a variable was successful

3)变量的先前分配是成功的

bar = [object myMethod];

if (bar)
{
    // If bar has a value associated to it that is not nil/false/zero
}
else
{
    // If bar equals nil, false or zero
}

#2


4  

This is all right but will generate a warning, since this is a common typo (= instead of ==). To silence that warning add another set of parentheses like this:

这样可以,但会产生警告,因为这是一个常见的拼写错误(=而不是==)。要使该警告静音,请添加另一组括号,如下所示:

if ((var = [object foo])) ...

Since this easily can lead to misunderstandings a lot of people will advise against doing this. For a simple if statement this is much clearer to do the assignment first:

由于这很容易导致误解,很多人会建议不要这样做。对于一个简单的if语句,首先进行赋值要清楚得多:

var = [object for];
if (var) ...

In while loops this is more useful, but also considered harmful by many people.

在while循环中,这更有用,但也被许多人认为是有害的。

#3


0  

use == instead of = in the if statement.

在if语句中使用==而不是=。

before the if statement, you may have var1 = [object foo] see comparison operators

在if语句之前,你可能有var1 = [object foo]看比较运算符

#4


0  

If you mean by valid that the variable contains an expected result, you can just perform another if on the variable against the expected result, or null to check it.

如果你的意思是变量包含一个预期的结果,那么你可以只根据预期的结果执行另一个变量,或者为null来检查它。

#1


2  

Not sure I understand your question, but let me try and explain a few situations you can check

我不确定我理解你的问题,但让我试着解释你可以检查的一些情况

1) Property contains value

1)财产包含价值

if ([object foo])
{
    // If foo has a value associated to it that is not nil/false/zero
}
else
{
    // If foo equals nil, false or zero
}

2) Assignment to a variable was successful

2)对变量的赋值是成功的

if ((bar = [object myMethod]))
{
    // If myMethod returns any non-nil value
}
else
{
    // If myMethod returns nil
}

3) Previous assignment of a variable was successful

3)变量的先前分配是成功的

bar = [object myMethod];

if (bar)
{
    // If bar has a value associated to it that is not nil/false/zero
}
else
{
    // If bar equals nil, false or zero
}

#2


4  

This is all right but will generate a warning, since this is a common typo (= instead of ==). To silence that warning add another set of parentheses like this:

这样可以,但会产生警告,因为这是一个常见的拼写错误(=而不是==)。要使该警告静音,请添加另一组括号,如下所示:

if ((var = [object foo])) ...

Since this easily can lead to misunderstandings a lot of people will advise against doing this. For a simple if statement this is much clearer to do the assignment first:

由于这很容易导致误解,很多人会建议不要这样做。对于一个简单的if语句,首先进行赋值要清楚得多:

var = [object for];
if (var) ...

In while loops this is more useful, but also considered harmful by many people.

在while循环中,这更有用,但也被许多人认为是有害的。

#3


0  

use == instead of = in the if statement.

在if语句中使用==而不是=。

before the if statement, you may have var1 = [object foo] see comparison operators

在if语句之前,你可能有var1 = [object foo]看比较运算符

#4


0  

If you mean by valid that the variable contains an expected result, you can just perform another if on the variable against the expected result, or null to check it.

如果你的意思是变量包含一个预期的结果,那么你可以只根据预期的结果执行另一个变量,或者为null来检查它。