7

Because my build machine is still using the Xcode 12.5 , So the UITabBar's scrollEdgeAppearance(which will not exist in the Xcode 12.5's SDK) will make the build fail even i'am using the @available to check .

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;
}

So is it possible to do this kind of SDK checking in code ,when the build SDK is not the newest SDK , these code will not be involved to build? like this

if (BuilDSDK >= someversion) 
{
   [UITabBar appearance].scrollEdgeAppearance = tabBarAppearance;
}
ximmyxiao
  • 2,622
  • 3
  • 20
  • 35

2 Answers2

5

@available is a runtime availability check, not really usable for compile-time stuff in this situation.

In Objective-C, you can wrap the code part applied on iOS 15 SDK into another, macro condition:

#ifdef __IPHONE_15_0
    if (@available(iOS 15.0, *)) {
        ...
    } else {
#endif
        // possible legacy branch code
#ifdef __IPHONE_15_0
    }
#endif

__IPHONE_15_0 is defined starting with iOS 15 SDK, thus being omitted when building in Xcode 12/iOS 14 SDK.

Michi
  • 1,464
  • 12
  • 15
3

Another similar solution for Swift classes can be found here: https://stackoverflow.com/a/69583441/1195661:

#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
palme
  • 2,499
  • 2
  • 21
  • 38