iOS/Objective-c: 如何在.h 文件中获取版本高于或等于13.0 的宏?
iOS/Objective-c: How to get a macro that says the version is higher or equal to 13.0 in the .h file?
我想要一个允许我检查版本或高于 ios13 的宏。我不想使用它:
if (@available(iOS 13, *)) {
// iOS 13 (or newer) ObjC code
} else {
// iOS 12 or older code
}
因为预处理器会尝试编译它而无法编译。
我想要的是一个允许在没有警告或错误消息的情况下正确编译的宏。
类似的东西:
#ifdef __IPHONE_13_0
但我不确定该行是否表示 13.0 或更高版本。
我在 .h 中的代码如下:
#ifdef __AVAILABILITY_INTERNAL__IPHONE_13_0
@property (nonatomic, retain) UIViewController *popoverController;
#else
@property (nonatomic, retain) UIPopoverController *popoverController;
#endif
没用。如何在 .h 文件中检查 ios 的版本?
我不能使用它,因为它在 .h 文件中:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
// code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
// code here
}
你怎么看?
编辑:
如何避免收到旧版本之前ipad的消息?
提前致谢。
但是您可以在定义 API
时使用预处理器变量
@property (nonatomic, retain) UIViewController *viewController NS_DEPRECATED_IOS(2.0, 13.0);
@property (nonatomic, retain) UIPopoverController *popoverController API_AVAILABLE(ios(13.0));
方法相同
-(void)halleluja:(Evil)brain API_AVAILABLE(ios(13.0)) {
//do evil stuff with brain
}
别忘了,这里指的是你的Apps目标系统版本。
您会在(上部文件部分)
#import "Availability.h"
但您不需要包含它,Xcode 会为您完成。
你也可以告诉你的预编译器
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_13
// code starting with ios(13.0)
[self halleluja:nope];
#else
// code earlier ios(13.0)
[self halleluja:papa]; //as defined above should throw a warning
#endif
最后你可以在运行时询问
if ([[[UIDevice currentDevice] systemVersion] isEqualToString:@"13.0"]) {
// code only on ios(13.0) and only ios(13.0)!
}
或
NSString *versionString = [[UIDevice currentDevice] systemVersion];
if ([versionString floatValue] >= 13.0) {
// not sexy - but works.
}
我想要一个允许我检查版本或高于 ios13 的宏。我不想使用它:
if (@available(iOS 13, *)) {
// iOS 13 (or newer) ObjC code
} else {
// iOS 12 or older code
}
因为预处理器会尝试编译它而无法编译。 我想要的是一个允许在没有警告或错误消息的情况下正确编译的宏。 类似的东西:
#ifdef __IPHONE_13_0
但我不确定该行是否表示 13.0 或更高版本。
我在 .h 中的代码如下:
#ifdef __AVAILABILITY_INTERNAL__IPHONE_13_0
@property (nonatomic, retain) UIViewController *popoverController;
#else
@property (nonatomic, retain) UIPopoverController *popoverController;
#endif
没用。如何在 .h 文件中检查 ios 的版本? 我不能使用它,因为它在 .h 文件中:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
// code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
// code here
}
你怎么看?
编辑: 如何避免收到旧版本之前ipad的消息?
但是您可以在定义 API
时使用预处理器变量@property (nonatomic, retain) UIViewController *viewController NS_DEPRECATED_IOS(2.0, 13.0);
@property (nonatomic, retain) UIPopoverController *popoverController API_AVAILABLE(ios(13.0));
方法相同
-(void)halleluja:(Evil)brain API_AVAILABLE(ios(13.0)) {
//do evil stuff with brain
}
别忘了,这里指的是你的Apps目标系统版本。
您会在(上部文件部分)
#import "Availability.h"
但您不需要包含它,Xcode 会为您完成。
你也可以告诉你的预编译器
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_13
// code starting with ios(13.0)
[self halleluja:nope];
#else
// code earlier ios(13.0)
[self halleluja:papa]; //as defined above should throw a warning
#endif
最后你可以在运行时询问
if ([[[UIDevice currentDevice] systemVersion] isEqualToString:@"13.0"]) {
// code only on ios(13.0) and only ios(13.0)!
}
或
NSString *versionString = [[UIDevice currentDevice] systemVersion];
if ([versionString floatValue] >= 13.0) {
// not sexy - but works.
}