ios - 是否可以根据 SDK 版本隐藏代码

标签 ios xcode xcodebuild

因为我的构建机器仍在使用 Xcode 12.5,所以即使我使用 @available 来检查,UITabBar 的 scrollEdgeAppearance(Xcode 12.5 的 SDK 中不存在)也会使构建失败。

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检查,当构建SDK不是最新的SDK时,这些代码将不会参与构建?像这样
if (BuilDSDK >= someversion) 
{
   [UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
}

最佳答案

@available是一个运行时可用性检查,在这种情况下对编译时的东西并不真正有用。
在 Objective-C 中,您可以将应用在 iOS 15 SDK 上的代码部分包装到另一个宏条件中:

#ifdef __IPHONE_15_0
    if (@available(iOS 15.0, *)) {
        ...
    } else {
#endif
        // possible legacy branch code
#ifdef __IPHONE_15_0
    }
#endif
__IPHONE_15_0从 iOS 15 SDK 开始定义,因此在 Xcode 12/iOS 14 SDK 中构建时被省略。 👍

关于ios - 是否可以根据 SDK 版本隐藏代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68798163/

相关文章:

ios - xcodebuild 生成与 Xcode 7.0.1 不同的存档

swift - xcodebuild -create-framework 错误 : unable to read the file

xcode - 如何过滤xcodebuild命令行输出?

objective-c - 多个模态视图可防止背景变暗/禁用 UIModalPresentationPageSheet

swift - 在这种 Firebase 情况下,如何在函数之间传递数据? swift + Xcode

ios - 域@count查询问题

ios - 带有标识符的应用ID不可用

ios - 无法为 'XCTest' 加载底层模块

iphone - 尽管使用 CGRectIntegral,但 UIScrollView 中的 UILabels 未对齐?

objective-c - 在NSString中复制,保留和引用计数