iOS: 处理 TARGET_IPHONE_SIMULATOR 宏
iOS: Handling TARGET_IPHONE_SIMULATOR macro
我的环境:
ObjectiveC 在 OS X El Captain (10.11.1) 中使用 Xcode 6.4。在 Xcode 中,目标设置为 iOS 8.
TARGET_IPHONE_SIMULATOR 在下面的代码中总是解析为真,即使我 select iPad2 作为 iOS 模拟器也是如此。
#if TARGET_IPHONE_SIMULATOR
// block of code
#endif
当select将iPad2用作iOS模拟器时,TARGET_IPHONE_SIMULATOR不应该设置为false吗?
该宏适用于任何模拟器构建。该宏早在 iPad 出现之前就已存在。回到 "iOS" 是 "iPhone OS".
的时候
因此可以将其视为 "TARGET_IOS_SIMULATOR"。
当您的代码中有某些内容只应在为模拟 iOS 设备构建时编译时使用。
如果您需要在 iPhone 模拟器和 iPad 模拟器之间 运行 不同的东西,您可能需要这样的东西:
#if TARGET_IPHONE_SIMULATOR
// This code is only for a simulator
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
// iPhone/iPod touch simulator
} else {
// iPad simulator
}
#endif
请注意,在 iOS 9 中引入了较新的宏以及 tvOS 和 watchOS,并且当时弃用了 TARGET_IPHONE_SIMULATOR 宏以帮助避免混淆。来自 TargetConditionals.h:
TARGET_OS_WIN32 - Generated code will run under 32-bit Windows
TARGET_OS_UNIX - Generated code will run under some Unix (not OSX)
TARGET_OS_MAC - Generated code will run under Mac OS X variant
TARGET_OS_IPHONE - Generated code for firmware, devices, or simulator
TARGET_OS_IOS - Generated code will run under iOS
TARGET_OS_TV - Generated code will run under Apple TV OS
TARGET_OS_WATCH - Generated code will run under Apple Watch OS
TARGET_OS_SIMULATOR - Generated code will run under a simulator
TARGET_OS_EMBEDDED - Generated code for firmware
TARGET_IPHONE_SIMULATOR - DEPRECATED: Same as TARGET_OS_SIMULATOR
TARGET_OS_NANO - DEPRECATED: Same as TARGET_OS_WATCH
对于任何使用 Swift 版本 >= 4.1 的开发者,最好使用 #if targetEnvironment(simulator)
。参考:Target environment platform condition
代码:
extension UIDevice {
static var isSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
我的环境: ObjectiveC 在 OS X El Captain (10.11.1) 中使用 Xcode 6.4。在 Xcode 中,目标设置为 iOS 8.
TARGET_IPHONE_SIMULATOR 在下面的代码中总是解析为真,即使我 select iPad2 作为 iOS 模拟器也是如此。
#if TARGET_IPHONE_SIMULATOR
// block of code
#endif
当select将iPad2用作iOS模拟器时,TARGET_IPHONE_SIMULATOR不应该设置为false吗?
该宏适用于任何模拟器构建。该宏早在 iPad 出现之前就已存在。回到 "iOS" 是 "iPhone OS".
的时候因此可以将其视为 "TARGET_IOS_SIMULATOR"。
当您的代码中有某些内容只应在为模拟 iOS 设备构建时编译时使用。
如果您需要在 iPhone 模拟器和 iPad 模拟器之间 运行 不同的东西,您可能需要这样的东西:
#if TARGET_IPHONE_SIMULATOR
// This code is only for a simulator
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
// iPhone/iPod touch simulator
} else {
// iPad simulator
}
#endif
请注意,在 iOS 9 中引入了较新的宏以及 tvOS 和 watchOS,并且当时弃用了 TARGET_IPHONE_SIMULATOR 宏以帮助避免混淆。来自 TargetConditionals.h:
TARGET_OS_WIN32 - Generated code will run under 32-bit Windows TARGET_OS_UNIX - Generated code will run under some Unix (not OSX) TARGET_OS_MAC - Generated code will run under Mac OS X variant TARGET_OS_IPHONE - Generated code for firmware, devices, or simulator TARGET_OS_IOS - Generated code will run under iOS TARGET_OS_TV - Generated code will run under Apple TV OS TARGET_OS_WATCH - Generated code will run under Apple Watch OS TARGET_OS_SIMULATOR - Generated code will run under a simulator TARGET_OS_EMBEDDED - Generated code for firmware TARGET_IPHONE_SIMULATOR - DEPRECATED: Same as TARGET_OS_SIMULATOR TARGET_OS_NANO - DEPRECATED: Same as TARGET_OS_WATCH
对于任何使用 Swift 版本 >= 4.1 的开发者,最好使用 #if targetEnvironment(simulator)
。参考:Target environment platform condition
代码:
extension UIDevice {
static var isSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}