objective-c - 为什么@YES 给出 "expected expression"错误,但 @(YES) 编译?

标签 objective-c compiler-errors objective-c-literals

<分区>

使用 XCode 4.4 的 Convert to Modern Objective C Syntax,我的 [NSNumber numberWithBool:YES] 调用被转换为 @(YES)。我有一些问题现在已经忘记了,我自己将它们更改为 @YES,这应该是正确的语法。

但是,这样做会给我错误:

Unexpected type name 'BOOL': expected expression

我知道有一个“表达式”语法,但我不明白为什么我不能简单地使用 @YES@NO

// Compiler error:
NSDictionary *userDefaultsDefaults = @{@"hasBeenLaunched": @YES};

// No error
NSDictionary *userDefaultsDefaults = @{@"hasBeenLaunched": @(YES)};

为什么 @(YES) 可以编译而 @YES 不能,我可以做些什么来补救?

最佳答案

简答:

使用@(YES)@(NO)


更长的答案:

看看this answer这说明这主要是 Apple 的疏忽。

关于 this answer 的评论者还指出:

There is one small thing I'd like to warn about. Literal bools are also not supported because of this. However, a quick fix that I implemented was adding this to the beginning of one of my common headers (in an iOS project)

#ifndef __IPHONE_6_0 
#if __has_feature(objc_bool) 
#undef YES 
#undef NO 
#define YES __objc_yes 
#define NO __objc_no 
#endif 
#endif

关于objective-c - 为什么@YES 给出 "expected expression"错误,但 @(YES) 编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11697189/

相关文章:

objective-c - 这是什么意思?

ios - swift 崩溃 : function signature specialization <Arg[0] = Owned To Guaranteed>

ios - UIScrollView 和 UIImageView - 水平移动

image - 无法在 GOLANG 中解码不同的图像格式(分配计数不匹配)

java - 如何从 actionPerformed 获取源对象,以便能够使用 getText() 方法并将文本从 JButton 输出到字符串文件?

objective-c - @[obj1, obj2] 创建什么样的对象?

objective-c - ManagedObjectContext 从 AppDelegate 返回 nil

iphone - 球绕圈弹跳

java - 错误 :failed to find target with hash string 'android-22'

objective-c - array[index] 和 [array objectAtIndex :index]? 有什么区别