title: Swift #available keyword vs respondsToSelector
body: <p>According to the <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-XID_8" rel="nofollow">pre-release Swift 2 documentation</a> there is now an <code>#available</code> keyword that can be used with an <code>if let</code> or <code>guard</code> statement for checking API availability. It gives the following example:</p>
  
  <pre><code>let locationManager = CLLocationManager()
  if #available(iOS 8.0, OSX 10.10, *) {
      locationManager.requestWhenInUseAuthorization()
  }
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>let locationManager = CLLocationManager()
  guard #available(iOS 8.0, OSX 10.10, *) else { return }
  locationManager.requestWhenInUseAuthorization()
  </code></pre>
  
  <p>Which it (the doc) states is the equivalent of the following Objective-C code:</p>
  
  <pre><code>if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) {
    // Method is available for use.
  } else {
    // Method is not available.
  }
  </code></pre>
  
  <p>Obviously <code>respondsToSelector:</code> only works on subclasses of <code>NSObject</code> whereas the <code>#available</code> keyword will work even for "pure" Swift code, so I appreciate that advantage.</p>
  
  <p>However since starting iOS development I've always been led to believe that the best practice for this situation is to detect the presence of an API rather than rely on the version it was introduced.</p>
  
  <p>As a more concrete example I'm thinking of when Apple introduced <code>firstObject</code> on <code>NSArray</code> in iOS 7 but retroactively made it available back to iOS 4 (where it was available, but private). Any code using <code>respondsToSelector:</code> would have worked on iOS &lt; 7 but obviously a version check would fail.</p>
  
  <p>Are there any benefits to shifting to the <code>#available</code> keyword that I've missed?</p>