如何比较@NO和@YES与优雅,没有误报/否定的风险?

时间:2022-07-18 04:06:42

I want to use bool literals like

我想使用bool文字

if (condition == @NO) {

}
else if (condition == @YES) {

{

When I try this, XCode wants me to use NSNumber methods to compare, like isEqualTo. Is there a simpler way to do this (without isEqualTo)? If I can't, should I use isEqualTo, isEqualToValue, or isEqualToNumber?

当我尝试这个时,XCode希望我使用NSNumber方法进行比较,比如isEqualTo。有没有更简单的方法(没有isEqualTo)?如果我不能,我应该使用isEqualTo,isEqualToValue还是isEqualToNumber?

5 个解决方案

#1


24  

What is condition? Is it a BOOL or an NSNumber?

条件是什么?它是BOOL还是NSNumber?


If condition is a BOOL, then you don't want to use @NO or @YES at all. You want to say

如果condition是BOOL,那么你根本不想使用@NO或@YES。你想说

if (condition) // test if condition is true

if (!condition) // test if condition is false

if (condition == NO) // same as previous, based on personal preference

Note that you should never say

请注意,你永远不应该说

if (condition == YES)

because BOOL isn't actually restricted to 0 and 1 as values, it can hold anything in char, so if condition accidentally holds, say, 3, then if (condition) and if (condition == YES) would behave differently.

因为BOOL实际上并不限制为0和1作为值,它可以在char中保存任何内容,因此如果条件意外地保持,例如3,则if(condition)和if(condition == YES)将表现不同。


If condition is an NSNumber, then you still don't want to use @NO and @YES. You just want to convert it to a BOOL using -boolValue, as in

如果condition是NSNumber,那么你仍然不想使用@NO和@YES。您只想使用-boolValue将其转换为BOOL,如

if ([condition boolValue]) // test if condition is true

if (![condition boolValue]) // test if condition is false

if ([condition boolValue] == NO) // same as previous, based on personal preference

The basic takeaway here is, don't use @NO and @YES literals for comparisons. It's pointless, and inelegant, since all you'd be able to do with them is convert them back into BOOLs.

这里的基本要点是,不要使用@NO和@YES文字进行比较。它毫无意义,而且不够优雅,因为你能用它们做的就是将它们转换回BOOL。

#2


4  

Assuming the data type of condition is BOOL, then you want:

假设条件的数据类型是BOOL,那么您需要:

if (condition) {
    // true (YES)
} else {
    // false (NO)
}

If condition is an NSNumber then use:

如果condition是NSNumber,那么使用:

if ([condition boolValue]) {
    // true (YES)
} else {
    // false (NO)
}

If condition is based on some arbitrary numbers then treat 0 as NO and treat non-zero as YES.

如果条件基于某些任意数字,则将0视为NO并将非零视为YES。

Again, if condition is an NSNumber then do:

同样,如果condition是NSNumber,那么:

if ([condition intValue]) {
    // non-zero - true (YES)
} else {
    // zero - false (NO)
}

Update: Based on the following comment from the OP:

更新:根据OP的以下评论:

Condition is a bool, like if (myView.frame.size.height == 30)

条件是一个bool,就像if(myView.frame.size.height == 30)

This implies that the actual question wanted to do checks something like:

这意味着实际的问题想要检查如下:

if ((myView.frame.size.height == 30) == YES)

This is very unusual. The proper thing to do is:

这很不寻常。正确的做法是:

if (myView.frame.size.height == 30) {
    // true - height is 30
} else {
    // false - height is not 30
}

There is no need to worry about false positives or negatives. The condition is either true or it isn't. There is no 3rd possibility. If the condition gives the wrong result then the solution is to fix the condition.

没有必要担心误报或否定。条件是真实的,也可能不是。没有第三种可能性。如果条件给出错误的结果,则解决方案是修复条件。

#3


2  

You can save @NO in a dictionary because @NO is an object. You cannot save NO in a dictionary. So use @NO.boolValue when needed. For example:

您可以将@NO保存在字典中,因为@NO是一个对象。你不能在字典中保存NO。因此,在需要时使用@ NO.boolValue。例如:

NSDictionary *userSetting = (NSDictionary *)[[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
//Suppose you have done: userSetting[@"canWriteCode"] = @YES;
if ([userSetting[@"canWriteCode"] boolValue])
    [self helloProgrammer];

#4


-1  

Is condition a NSNumber? If so just use:

条件是NSNumber吗?如果是这样,请使用:

if(condition.integerValue) {
    //condition is YES
} else {
    //condition is NO
}

And by the way, a value of 0 represents NO, any other value is YES;

顺便说一句,值0表示NO,任何其他值为YES;

#5


-2  

You can use @YES.boolValue and @NO.boolValue.

您可以使用@ YES.boolValue和@ NO.boolValue。

So, to compare against bool literal you can write something like (given the fact that condition is BOOL)

因此,为了与bool文字进行比较,您可以编写类似的内容(假设条件为BOOL)

if (condition == @NO.boolValue) {
  // Do something
}

#1


24  

What is condition? Is it a BOOL or an NSNumber?

条件是什么?它是BOOL还是NSNumber?


If condition is a BOOL, then you don't want to use @NO or @YES at all. You want to say

如果condition是BOOL,那么你根本不想使用@NO或@YES。你想说

if (condition) // test if condition is true

if (!condition) // test if condition is false

if (condition == NO) // same as previous, based on personal preference

Note that you should never say

请注意,你永远不应该说

if (condition == YES)

because BOOL isn't actually restricted to 0 and 1 as values, it can hold anything in char, so if condition accidentally holds, say, 3, then if (condition) and if (condition == YES) would behave differently.

因为BOOL实际上并不限制为0和1作为值,它可以在char中保存任何内容,因此如果条件意外地保持,例如3,则if(condition)和if(condition == YES)将表现不同。


If condition is an NSNumber, then you still don't want to use @NO and @YES. You just want to convert it to a BOOL using -boolValue, as in

如果condition是NSNumber,那么你仍然不想使用@NO和@YES。您只想使用-boolValue将其转换为BOOL,如

if ([condition boolValue]) // test if condition is true

if (![condition boolValue]) // test if condition is false

if ([condition boolValue] == NO) // same as previous, based on personal preference

The basic takeaway here is, don't use @NO and @YES literals for comparisons. It's pointless, and inelegant, since all you'd be able to do with them is convert them back into BOOLs.

这里的基本要点是,不要使用@NO和@YES文字进行比较。它毫无意义,而且不够优雅,因为你能用它们做的就是将它们转换回BOOL。

#2


4  

Assuming the data type of condition is BOOL, then you want:

假设条件的数据类型是BOOL,那么您需要:

if (condition) {
    // true (YES)
} else {
    // false (NO)
}

If condition is an NSNumber then use:

如果condition是NSNumber,那么使用:

if ([condition boolValue]) {
    // true (YES)
} else {
    // false (NO)
}

If condition is based on some arbitrary numbers then treat 0 as NO and treat non-zero as YES.

如果条件基于某些任意数字,则将0视为NO并将非零视为YES。

Again, if condition is an NSNumber then do:

同样,如果condition是NSNumber,那么:

if ([condition intValue]) {
    // non-zero - true (YES)
} else {
    // zero - false (NO)
}

Update: Based on the following comment from the OP:

更新:根据OP的以下评论:

Condition is a bool, like if (myView.frame.size.height == 30)

条件是一个bool,就像if(myView.frame.size.height == 30)

This implies that the actual question wanted to do checks something like:

这意味着实际的问题想要检查如下:

if ((myView.frame.size.height == 30) == YES)

This is very unusual. The proper thing to do is:

这很不寻常。正确的做法是:

if (myView.frame.size.height == 30) {
    // true - height is 30
} else {
    // false - height is not 30
}

There is no need to worry about false positives or negatives. The condition is either true or it isn't. There is no 3rd possibility. If the condition gives the wrong result then the solution is to fix the condition.

没有必要担心误报或否定。条件是真实的,也可能不是。没有第三种可能性。如果条件给出错误的结果,则解决方案是修复条件。

#3


2  

You can save @NO in a dictionary because @NO is an object. You cannot save NO in a dictionary. So use @NO.boolValue when needed. For example:

您可以将@NO保存在字典中,因为@NO是一个对象。你不能在字典中保存NO。因此,在需要时使用@ NO.boolValue。例如:

NSDictionary *userSetting = (NSDictionary *)[[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
//Suppose you have done: userSetting[@"canWriteCode"] = @YES;
if ([userSetting[@"canWriteCode"] boolValue])
    [self helloProgrammer];

#4


-1  

Is condition a NSNumber? If so just use:

条件是NSNumber吗?如果是这样,请使用:

if(condition.integerValue) {
    //condition is YES
} else {
    //condition is NO
}

And by the way, a value of 0 represents NO, any other value is YES;

顺便说一句,值0表示NO,任何其他值为YES;

#5


-2  

You can use @YES.boolValue and @NO.boolValue.

您可以使用@ YES.boolValue和@ NO.boolValue。

So, to compare against bool literal you can write something like (given the fact that condition is BOOL)

因此,为了与bool文字进行比较,您可以编写类似的内容(假设条件为BOOL)

if (condition == @NO.boolValue) {
  // Do something
}