是否可以根据SDK版本隐藏代码
Is it possible to hide code based on SDK version
因为我的构建机器仍在使用 Xcode 12.5,所以 UITabBar 的 scrollEdgeAppearance(在 Xcode 12.5 的 SDK 中不存在)将使构建失败,即使我是使用 @available 检查 .
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance* navBarAppearance = [UINavigationBarAppearance new];
navBarAppearance.backgroundColor = [UIColor colorNamed:@"navbar_bg"];
[UINavigationBar appearance].standardAppearance = navBarAppearance;
[UINavigationBar appearance].scrollEdgeAppearance = navBarAppearance;
UITabBarAppearance* tabBarAppearance = [UITabBarAppearance new];
tabBarAppearance.backgroundColor = [UIColor colorNamed:@"second_bg"];
[UITabBar appearance].standardAppearance = tabBarAppearance;
[UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
[UITableView appearance].sectionHeaderTopPadding = 0;
}
那么可不可以做这种SDK checking in code,当build SDK不是最新的SDK时,这些代码就不会参与build了?像这样
if (BuilDSDK >= someversion)
{
[UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
}
@available
是一个运行时可用性检查,在这种情况下对于编译时的东西并不真正有用。
在Objective-C中,可以将iOS15 SDK上应用的代码部分包装到另一个,宏条件:
#ifdef __IPHONE_15_0
if (@available(iOS 15.0, *)) {
...
} else {
#endif
// possible legacy branch code
#ifdef __IPHONE_15_0
}
#endif
__IPHONE_15_0
是从iOS15 SDK开始定义的,因此在Xcode12/iOS14 SDK中构建时被省略。
可在此处找到 Swift 类 的另一个类似解决方案::
#if swift(>=5.5) // Only run on Xcode version >= 13 (Swift 5.5 was shipped first with Xcode 13).
if #available(iOS 15.0, *) {
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}
#endif
因为我的构建机器仍在使用 Xcode 12.5,所以 UITabBar 的 scrollEdgeAppearance(在 Xcode 12.5 的 SDK 中不存在)将使构建失败,即使我是使用 @available 检查 .
if (@available(iOS 15.0, *)) {
UINavigationBarAppearance* navBarAppearance = [UINavigationBarAppearance new];
navBarAppearance.backgroundColor = [UIColor colorNamed:@"navbar_bg"];
[UINavigationBar appearance].standardAppearance = navBarAppearance;
[UINavigationBar appearance].scrollEdgeAppearance = navBarAppearance;
UITabBarAppearance* tabBarAppearance = [UITabBarAppearance new];
tabBarAppearance.backgroundColor = [UIColor colorNamed:@"second_bg"];
[UITabBar appearance].standardAppearance = tabBarAppearance;
[UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
[UITableView appearance].sectionHeaderTopPadding = 0;
}
那么可不可以做这种SDK checking in code,当build SDK不是最新的SDK时,这些代码就不会参与build了?像这样
if (BuilDSDK >= someversion)
{
[UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
}
@available
是一个运行时可用性检查,在这种情况下对于编译时的东西并不真正有用。
在Objective-C中,可以将iOS15 SDK上应用的代码部分包装到另一个,宏条件:
#ifdef __IPHONE_15_0
if (@available(iOS 15.0, *)) {
...
} else {
#endif
// possible legacy branch code
#ifdef __IPHONE_15_0
}
#endif
__IPHONE_15_0
是从iOS15 SDK开始定义的,因此在Xcode12/iOS14 SDK中构建时被省略。
可在此处找到 Swift 类 的另一个类似解决方案::
#if swift(>=5.5) // Only run on Xcode version >= 13 (Swift 5.5 was shipped first with Xcode 13).
if #available(iOS 15.0, *) {
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}
#endif