3 Replies
      Latest reply: Sep 29, 2016 3:04 AM by eskimo RSS
      eugenebut Level 1 Level 1 (0 points)

        Swift 3 documentation recommends using #available check for testing API availability in runtime. The arguments against using old weakly linked symbols approach are strong and go beyond Swift:

        • typing selectors is error prone
        • symbol could previously be a part of private API

        The same arguments can also be applied to Objective-C, but official Objective-C documentation still recommends using weakly linked symbols checks.

        Could you please clarify your recommendation for Objective-C. Is there a reason why developers should use different approach for Swift and Objective-C? Should Objective-C documentation be updated to use OS version checks? If so what would be your recommendation for code which is running on multiple platform (iOS, MacOS, WatchOS, etc…)?

         

        Thanks!

        • Re: Checking API Availability best practices
          eskimo Apple Staff Apple Staff (6,975 points)

          What you’re looking at here is an evolution of policy.  That is, the new policy was created as part of the Swift project and Swift supports it well.  Alas, that support has not been retrofitted to Objective-C, which makes it harder to use the new policy over there.  Thus, folks continue to use the old policy.

          Honestly, I don’t think the new policy is a sufficiently big win over the old policy that it justifies the extra work required to use it in Objective-C.  However, I agree that the documentation should be updated to at least acknowledge this discrepancy.  If you agree, you should file a bug against the docs.  Please post your bug number, just for the record.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Checking API Availability best practices
              eugenebut Level 1 Level 1 (0 points)

              Thank you, Quinn! Filed rdar://28533137 for documentation discrepancy.

               

              What do you think about API availability checks for multiplatform Objective-C code?

               

              Swift addresses this in very clean way:

              if #available(iOS 10, macOS 10.12, *)

               

              Do you think it would make sense to use the following code in Objective-C:

              if (IsRunningAtLeastIOS10() || IsRunningAtLeastMacOS10_12())

               

              Thanks,

              Eugene

                • Re: Checking API Availability best practices
                  eskimo Apple Staff Apple Staff (6,975 points)

                  What do you think about API availability checks for multiplatform Objective-C code?

                  Ah, yeah, that can be tricky.  The code you posted is perfectly reasonable.  Personally I generally do this stuff the old school way (-respondsToSelector: and so on) unless I ran into a specific problem with private APIs.  One less-obvious option is the framework version check.  Some frameworks include a version number and that version number usually makes sense in a cross-platform context.  For example, NSFoundationVersionNumber is in the 1200…1299 range for OS X 10.11 and iOS 9.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"