2012-07-28 13 views
5

Utilizzando la sintassi Convert to Modern Objective C di XCode 4.4, le mie chiamate [NSNumber numberWithBool:YES] sono state convertite in @(YES). Ho avuto qualche problema che ora ho dimenticato e li ho modificati personalmente su @YES, che dovrebbe essere la sintassi corretta.Perché @YES restituisce un errore di "espressione prevista", ma @ (YES) compila?

Tuttavia, così facendo mi dà l'errore:

Unexpected type name 'BOOL': expected expression

So che c'è una sintassi "espressione", ma non vedo perché non posso semplicemente utilizzare @YES e @NO.

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

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

Perché @(YES) compilare mentre @YES non lo fa, e cosa posso fare per porre rimedio a questo?

+0

Che cosa stai chiedendo esattamente? –

risposta

10

Risposta breve:

Usa @(YES) e @(NO)


Longer risposta:

Dai un'occhiata alla this answer che spiega che questo è in gran parte sembra essere una svista da parte di Apple.

Un commentatore this answer sottolinea inoltre:

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 
+0

Vedo. Quindi non sono pazzo. Grazie per il testa a testa. L'ho aggiunto al mio prefisso e sono stato in grado di utilizzare la sintassi non parentesi, come desiderato. – akaru

+1

Sembra che questo sia stato risolto in Xcode 4.5 - utilizzando fondamentalmente lo stesso codice, a giudicare dal [LLVM docs] (http://clang.llvm.org/docs/ObjectiveCLiterals.html). –

0

Le nuove letterali numerici (come @YES) nella aggiornamento Clang non sono pienamente attuati in XCode fino 4.5.

Problemi correlati