8
9

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 1 year has passed since last update.

Ubuntu 24.04 で Swift を使う

Last updated at Posted at 2017-12-26

OS の更新

sudo apt update
sudo apt upgrade

カーネルは 6.8.0-47-generic です。

前提ソフトのインストール

sudo apt install binutils \
          git \
          gnupg2 \
          libc6-dev \
          libcurl4-openssl-dev \
          libedit2 \
          libgcc-13-dev \
          libncurses-dev \
          libpython3-dev \
          libsqlite3-0 \
          libstdc++-13-dev \
          libxml2-dev \
          libz3-dev \
          pkg-config \
          tzdata \
          unzip \
          zlib1g-dev

ダウンロード

wget https://download.swift.org/swift-6.0.1-release/ubuntu2204/swift-6.0.1-RELEASE/swift-6.0.1-RELEASE-ubuntu22.04.tar.gz

解凍

tar xzf swift-6.0.1-RELEASE-ubuntu24.04.tar.gz

パスの設定

解凍したフォルダに対して シンボリックリンクを /opt/swift に張る

export PATH=/opt/swift/usr/bin:"${PATH}"

インストールしたら起動の確認

$ swift --version
Swift version 6.0.1 (swift-6.0.1-RELEASE)
Target: x86_64-unknown-linux-gnu

簡単な計算

$ swift repl
Welcome to Swift version 6.0.1 (swift-6.0.1-RELEASE).
Type :help for assistance.
  1> 1 + 2 + 3
$R0: Int = 6
  2> :quit

プログラム

ex01.swift
import Foundation

let d=Date()

print("現在時刻")
print(d)

print(d.timeIntervalSince1970)
print("完了")

実行結果

$ swift ex01.swift 
現在時刻
2024-10-17 05:24:02 +0000
1729142642.2236345
完了

テキストファイルの読み込み

text_read.swift
// -----------------------------------------------------------------
//
//	text_read.swift
//
//						Oct/17/2024
// -----------------------------------------------------------------
import Foundation

let arguments = CommandLine.arguments
let fileName = arguments[1]

let file = FileHandle(forReadingAtPath: fileName)!

let contentData = file.readDataToEndOfFile()

let contentString = String(data: contentData, encoding: .utf8)!

file.closeFile()

print(contentString)
// -----------------------------------------------------------------

実行方法

swift text_read.swift file_example.txt

参考ページ

Download Swift
Arch Linux で Swift を使う

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?