Menu
CoddyTech
コース

C++開発者のためのSwift

C++の経験を活かして、Swiftの値セマンティクス、オプショナル、Unicode、プロトコル、ARCを学びましょう。48個のマルチファイル課題、4ステップで進める小包受付プロジェクト、そして2つの完全なC++プログラムの移植に取り組みます。

C++とSwiftを並べて比較

このコースのレッスンから、同じプログラムをC++とSwiftで並べています。

  1. バインディングとミューテーション

    C++の auto は型推論を要求するものであり、それ自体がローカル変数を不変にするわけではありません。const が別個の制限を提供します。Swiftも同様に固定の型を推論しますが、let と var を明示的に区別します。C++の const オブジェクトは実行時に初期化できますが、constexpr は追加の定数式ルールを適用します。Swiftの let…

    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <map>5#include <set>6#include <optional>7#include <memory>8#include <tuple>9#include <variant>10#include <stdexcept>11#include <algorithm>12
    13int main() {14auto mode = std::string("draft");15    mode = "ready";16    const auto label = mode;17    std::cout << label << '\n';18}
    Swift
    1var mode = "draft"2mode = "ready"3let label = mode4print(label)
  2. 文字列と補間

    C++では、ストリーム、文字列の連結、またはフォーマットライブラリを使って書式付きテキストを作成できます。一方、Swiftではバックスラッシュと括弧を使って、String内に式を直接埋め込めます。std::stringは値セマンティクスと変更可能な文字ストレージを備えており、コピーしても通常、同じ変更可能な文字列オブジェクトへの2つ目の参照は作成されません。SwiftのStringも値セマンティクスを備えていますが、整数でバイトをインデックス指定するのではなく、Characterに基づく走査を提供します。表示されて…

    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <map>5#include <set>6#include <optional>7#include <memory>8#include <tuple>9#include <variant>10#include <stdexcept>11#include <algorithm>12
    13int main() {14int side = 4;15    std::cout << side << " squared = " << side * side << '\n';16}
    Swift
    1let side = 42print("\(side) squared = \(side * side)")
  3. Switchとパターン

    C++のswitchは整数型または列挙型の条件を受け入れ、制御が脱出しない限りcaseの処理本体間でのフォールスルーを許可します。Swiftのswitchは文字列やより豊かなパターンをサポートし、自動的にcaseの処理を完了します。その網羅性は、すべての可能な入力を含めるか、defaultを設定する必要があります。C++側は1バイトのコマンド文字を比較し、Swift側は文字列全体のコマンドを受け入れます。隣接するC++のcaseラベルは1つの処理本体を共有できますが、Swiftは1つのcase内に複数のパターンをま…

    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <map>5#include <set>6#include <optional>7#include <memory>8#include <tuple>9#include <variant>10#include <stdexcept>11#include <algorithm>12
    13int main() {14char command = 'w';15    switch (command) {16        case 'w': case 's': std::cout << "write\n"; break;17        default: std::cout << "ignore\n"; break;18    }19}
    Swift
    1let command = "w"2switch command {3case "w", "save": print("write")4default: print("ignore")5}
  4. 関数とラベル

    C++とSwiftはどちらも自由関数とデフォルト引数をサポートしていますが、通常のC++の呼び出しではSwiftの外部引数ラベルは使いません。Swiftでは、呼び出し側のラベルと関数内のパラメーター名を区別します。アンダースコアを使うと、その外部ラベルを省略できます。Swiftの関数では、明示的なローカルコピーやinoutの契約によって設計を変えない限り、パラメーターは定数の束縛です。例では区切り文字を挟んで連結したテキストを返します。出力するのは呼び出し側の責任です。timesのような必須ラベルがある場合、パラメ…

    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <map>5#include <set>6#include <optional>7#include <memory>8#include <tuple>9#include <variant>10#include <stdexcept>11#include <algorithm>12std::string repeat_text(const std::string &text, int times) {13    std::string result;14    for (int i = 0; i < times; ++i) { if (i) result += "/"; result += text; }15    return result;16}17
    18int main() {19std::cout << repeat_text("ha", 3) << '\n';20}
    Swift
    1func repeatText(_ text: String, times: Int) -> String {2    return Array(repeating: text, count: times).joined(separator: "/")3}
  5. 辞書と存在しないキー

    C++ の std::map::operator[] は、存在しないキーにデフォルト値を挿入します。一方、find は挿入せずに不在を確認し、at はエントリが存在しない場合に例外をスローします。Swift の Dictionary で通常の検索を行うと Optional が返され、サブスクリプトを通じて nil を代入すると、そのマッピングが削除されます。読み取りによって状態が変化する可能性があるかどうかに応じて、適切な検索 API を選びましょう。この例では zoom を削除し、代わりに 100…

    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <map>5#include <set>6#include <optional>7#include <memory>8#include <tuple>9#include <variant>10#include <stdexcept>11#include <algorithm>12
    13int main() {14std::map<std::string, int> settings{{"zoom", 125}};15    settings.erase("zoom");16    auto found = settings.find("zoom");17    std::cout << (found == settings.end() ? 100 : found->second) << '\n';18}
    Swift
    1var settings = ["zoom": 125]2settings["zoom"] = nil3print(settings["zoom"] ?? 100)
  6. 構造体と独立したコピー

    C++のデフォルトのメンバーごとのコピーとSwiftの構造体の代入は、個々のフィールドのセマンティクスを保持します。std::string メンバーはその値をコピーする一方、shared_ptr メンバーは同じ参照先の共有所有権を保持します。Swiftの String 値とクラス参照フィールドにも、この例で同様の区別があります。コピー側の label を変更しても元の label は影響を受けませんが、共有されている cell…

    C++
    1#include <iostream>2#include <string>3#include <vector>4#include <map>5#include <set>6#include <optional>7#include <memory>8#include <tuple>9#include <variant>10#include <stdexcept>11#include <algorithm>12struct Cell { std::string text; };13struct LabelledBox { std::string label; std::shared_ptr<Cell> shared; };14
    15int main() {16LabelledBox original{"old", std::make_shared<Cell>(Cell{"one"})};17    auto copy = original;18    copy.label = "new";19    copy.shared->text = "two";20    std::cout << original.label << ' ' << original.shared->text << '\n';21}
    Swift
    1final class Cell { var text: String; init(text: String) { self.text = text } }

シラバス

10 章48 レッスン1 プロジェクト48 チャレンジ99 クイズ問題

荷物管理デスク・プロジェクト

4 レッスン

C++開発者向けのほかのコース

ほかの言語からSwiftを学ぶ

よくある質問

C++開発者のためのSwiftはどんな人向けですか?

すでにC++を書けて、Swiftを学びたい人向けです。各レッスンは知っているC++のコードから始まり、Swiftで同じことをどう書くかを示すので、初心者向けの基礎は飛ばせます。

Swiftの知識は必要ですか?

いいえ。コースは最初のSwiftプログラムから始まります。必要なのはC++の基礎(変数、条件分岐、ループ、関数、コレクション)です。

C++開発者のためのSwiftにはレッスンがいくつありますか?

C++開発者のためのSwiftには48のインタラクティブなレッスンがあり、自分のペースで進められます。

C++開発者のためのSwiftでは何を学べますか?

コースは次の章で構成されています: Swiftをはじめよう, テキストと条件分岐, 制御フロー, 関数と配列, and more。

C++開発者のためのSwiftは無料ですか?

はい。Coddyのすべてのコースは無料で開始できます。レッスンはブラウザで進められ、ダウンロードも登録も不要です。

Coddyのプログラミング言語のイラスト

C++開発者のためのSwiftを無料で始める

始める