目标iPhone模拟器宏不起作用

Kev*_*tre 58 iphone cocoa-touch

使用TARGET_IPHONE_SIMULATOR宏会导致在am应用程序中定义相同的常量值.例如:

#ifdef TARGET_IPHONE_SIMULATOR
NSString * const Mode = @"Simulator";
#else
NSString * const Mode = @"Device";
#endif

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
   ...
   NSLog(@"Mode: %@", Mode);
   ...
}
Run Code Online (Sandbox Code Playgroud)

始终导致记录"模式:模拟器".我正在运行XCode 3.2.4,如果这有帮助的话.谢谢.

Kev*_*tre 106

TARGET_OS_SIMULATOR在设备上定义(但定义为false).修复是:

#include <TargetConditionals.h> // required in Xcode 8+

#if TARGET_OS_SIMULATOR
NSString * const Mode = @"Simulator";
#else
NSString * const Mode = @"Device";
#endif
Run Code Online (Sandbox Code Playgroud)

不知道什么时候改变了.我很确定过去可以使用'ifdef'.

  • 谢谢你的话题; 我以为我疯了,或者更糟(忘了我多年的C开发). (3认同)

Seb*_*ian 13

对我来说明确包括TargetConditionals.h帮助

#include <TargetConditionals.h>
Run Code Online (Sandbox Code Playgroud)


小智 5

尝试TARGET_OS_SIMULATOR,因为不推荐使用TARGET_IPHONE_SIMULATOR.