Swift for C++ Developers
Bring C++ experience to Swift value semantics, optionals, Unicode, protocols and ARC. Complete 48 multi-file tasks, a four-step parcel desk project and two complete C++ program ports.
C++ and Swift side by side
The same program in C++ and in Swift, from the lessons of this course.
Bindings and Mutation
C++ auto requests type inference and does not itself make a local immutable; const supplies a separate restriction. Swift similarly infers a fixed type but explicitly distinguishes let from var. A C++ const object may be initialized at runtime, while…
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}Swift1var mode = "draft"2mode = "ready"3let label = mode4print(label)Strings and Interpolation
C++ can build formatted text with streams, concatenation or a formatting library, while Swift directly interpolates expressions inside String using a backslash and parentheses. std::string has value semantics and mutable character storage; copying it does not…
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}Swift1let side = 42print("\(side) squared = \(side * side)")Switch and Patterns
C++ switch accepts integral or enumeration conditions and permits fallthrough between case bodies unless control exits. Swift switch supports strings and richer patterns and finishes a case automatically. Its coverage must include all possible inputs or a…
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}Swift1let command = "w"2switch command {3case "w", "save": print("write")4default: print("ignore")5}Functions and Labels
C++ and Swift both support free functions and default arguments, but ordinary C++ calls do not use Swift external argument labels. Swift distinguishes the label at the call site from the parameter name within the function; an underscore suppresses that…
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}Swift1func repeatText(_ text: String, times: Int) -> String {2 return Array(repeating: text, count: times).joined(separator: "/")3}Dictionaries and Missing Keys
C++ std::map::operator[] inserts a default value for a missing key, while find observes absence without inserting and at throws for a missing entry. Swift Dictionary ordinary lookup returns an Optional, and assigning nil through its subscript removes the…
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}Swift1var settings = ["zoom": 125]2settings["zoom"] = nil3print(settings["zoom"] ?? 100)Structs and Independent Copies
C++ default memberwise copying and Swift struct assignment preserve the semantics of their individual fields. A std::string member copies its value, while a shared_ptr member retains shared ownership of the same pointee. Swift String values and…
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}Swift1final class Cell { var text: String; init(text: String) { self.text = text } }
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsStarting Swift
5 lessonsText and Decisions
5 lessonsControl Flow
5 lessonsFunctions and Arrays
5 lessonsCollection Tools
5 lessonsValues and References
5 lessonsFailures and Abstraction
6 lessonsDesigning Types
6 lessonsParcel Desk Project
4 lessonsC++ Ports
2 lessonsFrequently asked questions
Who is Swift for C++ Developers for?
For people who already write C++ and want to learn Swift. Each lesson starts from C++ you know and shows how Swift does the same thing, so you skip the beginner basics.
Do I need to know Swift already?
No. The course starts with your first Swift program. You need C++ basics: variables, conditions, loops, functions and collections.
How many lessons does Swift for C++ Developers have?
Swift for C++ Developers contains 48 interactive lessons you can complete at your own pace.
What does Swift for C++ Developers cover?
The course is organized into chapters covering: Starting Swift, Text and Decisions, Control Flow, Functions and Arrays, and more.
Is Swift for C++ Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.