8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】ターミナルでバージョン確認

8
Last updated at Posted at 2020-04-05

概要

Swiftの勉強をはじめました。
当たり前のことですが、自分が扱うSwiftのバージョンを把握しておかないと学習も進まない訳で…
という訳で、ここではターミナルを使ったバージョン確認の方法を備忘録としてまとめています。

筆者環境

  • MacBook Pro (13-inch, 2019)
  • macOS Catalina(Version 10.15.4)
  • Xcode(Version 11.4)

バージョン確認

ターミナルを起動し、以下のコマンドを入力することで、Swiftのバージョンを確認することができます。

 $ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -version

結果

Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
Target: x86_64-apple-darwin19.4.0

Swiftのバージョンが"5.2"であることが確認できました。

【おまけ】PATHを通す

上記の通りでもバージョン確認はできますが、一度PATHを通しておくと色々便利ですね。
僕はまだだったので以下の手順でPATHを通しました。

Swiftコマンド実行ファイルの格納場所を確認

ターミナルで以下を実行します。

$ which swift

結果

/usr/bin/swift

設定ファイルの編集

ターミナルで以下を実行します。

$ vi ~/.bash_profile

設定ファイルが開くので、以下を追加します。

# 編集開始 i

export PATH=$PATH:/usr/bin/swift

# 保存 esc → :wq
# 保存せずに終了 esc → :qa!

設定ファイルの更新

設定の変更を反映させるために以下を実行

$ source ~/.bash_profile

バージョン確認

PATHが通ったかの確認も含めて、バージョン確認します。

$ swift -version

結果

Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
Target: x86_64-apple-darwin19.4.0

これで完了です!

【おまけのおまけ】PATHを通した後のエラー

PATHを通して、一度目のバージョン確認の時、以下のようなエラーが出ました。

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

どうやら直前にmacOSをアップロードしていたのが原因のよう。
以下のコマンドを実行し、Xcodeをインストールすることで解決し、無事PATHが通りました。

 $ xcode-select --install
8
2
0

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
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?