Objective-C Bool文字和宏

时间:2021-04-25 22:47:02

Disclaimer: limited knowledge of Objective-C (passing curiosity).

免责声明:对Objective-C(传递好奇心)的了解有限。

if (@YES)

vs

vs

if (YES)

What's the difference?

有什么区别呢?

From what I understand, @YES is a bool object literal, and YES is a macro that expands to 1? Is this correct? If so, why use @YES instead of YES or vice versa?

根据我的理解,@YES是bool对象的文本,YES是扩展为1的宏吗?这是正确的吗?如果是,为什么用@YES而不是YES,反之亦然?

3 个解决方案

#1


6  

@YES is a shortcut for [NSNumber numberWithBool:YES], so this:

@YES是[NSNumber withbool:YES]的快捷方式,因此:

if (@YES)

actually means

实际上意味着

if ([NSNumber numberWithBool:YES] != nil)

Note, that it doesn't matter what number (or bool value) that is. You just test that it's a valid object. Even @NO will evaulate to YES if you test it like that, because it's a non-nil NSNumber instance:

注意,这与数字(或bool值)无关。你只要测试它是一个有效的对象。即使是@NO如果你这样测试它也会得到YES,因为它是非nil NSNumber实例:

if (@NO) NSLog(@"test");

Output:

输出:

2013-12-07 21:02:49.828 MyApp[37512:70b] test

Long story short: Don't use @YES or @NO like that, they will not behave as you would expect.

长话短说:不要像那样使用@YES或@NO,它们的行为不会像你预期的那样。

#2


1  

The @ symbol is a recent addition to Objective-C, which changes literals into their object representation in the form of an NSNumber instance.

@符号是Objective-C的最新添加,它以NSNumber实例的形式将文字转换为它们的对象表示。

This is especially useful if you need to store literals in arrays and dictionaries, which can only store objects (type id).

如果需要将文字存储在数组和字典中,这是特别有用的,因为它们只能存储对象(类型id)。

The way you use it, there is not need to create an object for the literal, so you should just use the literal directly.

使用它的方法是,不需要为文字创建对象,所以应该直接使用文字。

The @ symbol has always been used for denoting NSString objects, and only 2-3 years ago this syntax was extended for literals and expressions.

@符号一直用于表示NSString对象,仅在2-3年前,这种语法被扩展为文字和表达式。

#3


1  

@YES, @1, @1.5...these are all literal syntax that are the equivalent of an NSNumber ie: [NSNumber numberWithBool:YES], [NSNumber numberWithInt:1], [NSNumber numberWithDouble:1.5]. Using this means you are creating an NSNumber object.

@YES @1,@1.5……这些都是字面上的语法,相当于一个NSNumber ie: [NSNumber numberWithBool:YES], [NSNumber numberWithInt:1], [NSNumber numberWithDouble:1.5]。使用它意味着您正在创建一个NSNumber对象。

YES is simply the BOOL type, it's a primitive type in Obj-C, not an object like NSNumber. The BOOL type is used in Objective-C to hold true or false values. This is what you would normally use. You would only use @YES in cases where you need to hold the primitives in something that only accepts objects, perhaps if you wanted to hold them in an NSArray for example.

YES只是BOOL类型,它是object - c中的原始类型,而不是像NSNumber这样的对象。BOOL类型在Objective-C中用于保存真值或假值。这是你通常会用到的。如果您需要在只接受对象的东西中保存原语,比如希望在NSArray中保存原语,那么您只能使用@YES。

#1


6  

@YES is a shortcut for [NSNumber numberWithBool:YES], so this:

@YES是[NSNumber withbool:YES]的快捷方式,因此:

if (@YES)

actually means

实际上意味着

if ([NSNumber numberWithBool:YES] != nil)

Note, that it doesn't matter what number (or bool value) that is. You just test that it's a valid object. Even @NO will evaulate to YES if you test it like that, because it's a non-nil NSNumber instance:

注意,这与数字(或bool值)无关。你只要测试它是一个有效的对象。即使是@NO如果你这样测试它也会得到YES,因为它是非nil NSNumber实例:

if (@NO) NSLog(@"test");

Output:

输出:

2013-12-07 21:02:49.828 MyApp[37512:70b] test

Long story short: Don't use @YES or @NO like that, they will not behave as you would expect.

长话短说:不要像那样使用@YES或@NO,它们的行为不会像你预期的那样。

#2


1  

The @ symbol is a recent addition to Objective-C, which changes literals into their object representation in the form of an NSNumber instance.

@符号是Objective-C的最新添加,它以NSNumber实例的形式将文字转换为它们的对象表示。

This is especially useful if you need to store literals in arrays and dictionaries, which can only store objects (type id).

如果需要将文字存储在数组和字典中,这是特别有用的,因为它们只能存储对象(类型id)。

The way you use it, there is not need to create an object for the literal, so you should just use the literal directly.

使用它的方法是,不需要为文字创建对象,所以应该直接使用文字。

The @ symbol has always been used for denoting NSString objects, and only 2-3 years ago this syntax was extended for literals and expressions.

@符号一直用于表示NSString对象,仅在2-3年前,这种语法被扩展为文字和表达式。

#3


1  

@YES, @1, @1.5...these are all literal syntax that are the equivalent of an NSNumber ie: [NSNumber numberWithBool:YES], [NSNumber numberWithInt:1], [NSNumber numberWithDouble:1.5]. Using this means you are creating an NSNumber object.

@YES @1,@1.5……这些都是字面上的语法,相当于一个NSNumber ie: [NSNumber numberWithBool:YES], [NSNumber numberWithInt:1], [NSNumber numberWithDouble:1.5]。使用它意味着您正在创建一个NSNumber对象。

YES is simply the BOOL type, it's a primitive type in Obj-C, not an object like NSNumber. The BOOL type is used in Objective-C to hold true or false values. This is what you would normally use. You would only use @YES in cases where you need to hold the primitives in something that only accepts objects, perhaps if you wanted to hold them in an NSArray for example.

YES只是BOOL类型,它是object - c中的原始类型,而不是像NSNumber这样的对象。BOOL类型在Objective-C中用于保存真值或假值。这是你通常会用到的。如果您需要在只接受对象的东西中保存原语,比如希望在NSArray中保存原语,那么您只能使用@YES。