LoginSignup
10

More than 5 years have passed since last update.

Swift APIのavailabilityチェックはextensionでもできた〜

Posted at

状況に応じて #available@available を使うことで、OSのバージョンによって処理を分けることができます。が、今日、コードを書いていてこれがextension単位でも効くことを知りました。

ドキュメントにも載ってる頻出の使い方

guard #available(iOS 8.0, OSX 10.10, *) else { return }
@available(iOS 8.0, OSX 10.10, *)
func useShinyNewFeature() {
    // ...
}

extensionでも使える

// MARK:-
// MARK: SHORTCUT
@available(iOS 9.0, *)
extension AppDelegate {
    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        ...
    }
}

これはちょっとうれしかった。

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10