Mac SDK: using latest SDK but ensuring backwards compatibility with earlier deployment target

后端 未结 6 912
死守一世寂寞
死守一世寂寞 2020-12-31 06:39

As always when Apple updates OS X, the latest XCode 4.4 dumps the older (10.6) SDK and I find myself needing to use the 10.7 SDK (or 10.8 I suppose) and setting my deploymen

6条回答
  •  没有蜡笔的小新
    2020-12-31 07:06

    i also assumed that the compiler will warn me about "too new" API usage for the deployment target OS version. but it turned out that the compiler doesn't warn you about it by default. one of the reasons might be you could still use the new API by checking the availability during runtime with "respondsToSelector:", for example, on a newer OS version even when the deployment target version was older. you would need to add the compiler option -Wpartial-availability which is available on Xcode 7.3+ (to be confirmed) in order to get the "warning: 'something' is partial: introduced in macOS 10.x" warning message.

    on macOS 10.12.3 with Xcode 8.2.1:

    $ cat foo.m
    #include 
    
    BOOL foo()
    {
        return [@"foo" containsString:@"bar"];
    }
    
    $ cc -mmacosx-version-min=10.9 -Wpartial-availability foo.m -c -o foo.o
    foo.m:5:20: warning: 'containsString:' is partial: introduced in macOS 10.10 [-Wpartial-availability]
        return [@"foo" containsString:@"bar"];
                       ^
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h:132:1: note: 
          'containsString:' has been explicitly marked partial here
    - (BOOL)containsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);
    ^
    foo.m:5:20: note: explicitly redeclare 'containsString:' to silence this warning
        return [@"foo" containsString:@"bar"];
                       ^
    1 warning generated.
    

    see also: Is there a way for XCode to warn about new API calls?

提交回复
热议问题