🐻‍❄️

Swiftで何のFeature Flagが有効になっているかを調べたいときにhasFeatureを利用する

に公開

はじめに

開発しているプロジェクトのSwiftコード上で、何のフラグが有効になっているかを知りたい時があると思うのでそれだけ書いておきます。#if hasFeature()でできます。

解決策: #if hasFeatureを使う

具体例としてNonisolatedNonsendingByDefaultが有効かを調べる例を示します。

#if hasFeature(NonisolatedNonsendingByDefault)
 // SE-0461 有効
 print("NonisolatedNonsendingByDefault 🥰")
#else
 // SE-0461 無効
 print("NonisolatedNonsendingByDefault 🫥")
#endif

そもそもの課題

Xcode 26の設定は複数のフラグをオンにしてしまう

何のフラグが有効か分からないことのほかに、Xcode 26のBetaなどではBuild Settings > Swift Compiler - Concurrency > Approachable Concurrency設定においては、YESの場合に複数のUpcoming Feature Flagが有効になり、Xcodeの設定上から特定のFlagの設定有無が見えないこともあるからです。

参考

#if hasFeatureについて

https://github.com/swiftlang/swift-evolution/blob/main/proposals/0362-piecemeal-future-features.md

Approachable Concurrencyについて

https://useyourloaf.com/blog/approachable-concurrency-in-swift-packages/

Discussion