为什么@YES会给出“期望的表达式”错误,但@(是)编译?
使用XCode 4.4的转换为现代Objective C语法,我的[NSNumber numberWithBool:YES]
调用转换为@(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其解释说,这主要是似乎是苹果公司的疏忽。
上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
我明白了。所以我并不疯狂。感谢您的高举。我将它添加到我的前缀中,并能够根据需要使用非括号语法。 – akaru 2012-07-28 02:04:38
看起来这是在Xcode 4.5中修复的 - 使用基本相同的代码,由[LLVM文档](http://clang.llvm.org/docs/ObjectiveCLiterals.html)来判断。 – 2012-07-29 05:31:34
新号码文字(如@YES)在Clang的更新并没有完全落实到了XCode 4.5为止。
你到底在问什么? – 2012-07-28 00:30:19