Courses/Swift/Introduction to Swift

    Lesson 1 • Beginner

    Introduction to Swift 🦅

    Discover why Apple created Swift, how it powers iOS, macOS, watchOS, and beyond — and write your very first line of Swift code.

    What You'll Learn in This Lesson

    • • What Swift is and why Apple built it
    • • The difference between let (constant) and var (variable)
    • • Swift's type inference and type safety systems
    • • String interpolation with \(expression)
    • • How to set up Xcode or an online Swift playground

    1️⃣ What is Swift?

    Swift is Apple's modern programming language for building apps on iOS, macOS, watchOS, tvOS, and even server-side applications. It was introduced at WWDC 2014 as a replacement for Objective-C, offering cleaner syntax, better safety, and blazing performance. Swift is open source since 2015 and runs on Linux too.

    Key features that make Swift special: type safety (catches errors at compile time), optionals (handles missing values safely), protocol-oriented programming, and Automatic Reference Counting (ARC) for memory management.

    2️⃣ Your First Swift Program

    Every programming journey starts with "Hello, World!" In Swift, it's just one line: print("Hello, World!"). No semicolons needed, no boilerplate — Swift keeps things clean and readable.

    Try It: Hello Swift

    Write your first Swift-style program with variables, types, and string interpolation

    Try it Yourself »
    JavaScript
    // Your First Swift Program — simulated in JavaScript
    console.log("=== Hello, Swift! ===");
    console.log();
    
    // In Swift you write:
    //   print("Hello, World!")
    // Let's simulate Swift's print function
    function print(message) {
      console.log(message);
    }
    
    print("Hello, World!");
    print("Welcome to Swift programming!");
    console.log();
    
    // Swift is strongly typed but uses type inference
    // let name: String = "Alice"   — explicit type
    // let name = "Alice"           — inferred as String
    
    let name = "Al
    ...

    3️⃣ Swift Playground — Arithmetic & Types

    Swift supports all standard arithmetic operators and enforces strict type safety. You can't accidentally add an Int to a Double without explicit conversion — this prevents subtle bugs that plague other languages.

    Try It: Swift Playground

    Explore arithmetic, type safety, and multiline strings

    Try it Yourself »
    JavaScript
    // Swift Playground — Explore Basic Operations
    console.log("=== Swift Arithmetic ===");
    
    let a = 42;
    let b = 7;
    console.log(a + " + " + b + " = " + (a + b));
    console.log(a + " - " + b + " = " + (a - b));
    console.log(a + " * " + b + " = " + (a * b));
    console.log(a + " / " + b + " = " + (a / b));
    console.log(a + " % " + b + " = " + (a % b));
    console.log();
    
    // Type safety — Swift won't let you mix types
    console.log("=== Type Safety ===");
    let intVal = 10;
    let doubleVal = 3.14;
    // In Swift: let res
    ...

    4️⃣ Setting Up Your Environment

    You have several options for writing and running Swift code. Xcode is the gold standard for iOS/macOS development, but you can also use the Swift REPL in Terminal or online playgrounds to get started without any installation.

    Try It: Setup Guide

    See all the ways to set up and run Swift on your machine

    Try it Yourself »
    JavaScript
    // Setting Up Your Swift Environment
    console.log("=== How to Run Swift ===");
    console.log();
    
    console.log("Option 1: Xcode (macOS only)");
    console.log("  1. Download Xcode from the Mac App Store");
    console.log("  2. Open Xcode → File → New → Playground");
    console.log("  3. Choose 'Blank' template → Create");
    console.log("  4. Write code and press ▶️ to run");
    console.log();
    
    console.log("Option 2: Swift REPL (Terminal)");
    console.log("  1. Open Terminal on macOS");
    console.log("  2. Type: swift"
    ...

    ⚠️ Common Mistakes

    ⚠️
    Reassigning a let constantlet x = 5; x = 10 will cause a compile error. Use var for values that change.
    ⚠️
    Mixing types without conversionlet sum = 5 + 3.14 won't compile. Use Double(5) + 3.14.
    💡
    Pro Tip: Prefer let over var whenever possible. Immutable values are safer and help the compiler optimize your code.

    📋 Quick Reference — Swift Basics

    ConceptSwift Syntax
    Constantlet name = "Alice"
    Variablevar score = 0
    Explicit typelet age: Int = 25
    String interpolationprint("Hi, \(name)!")
    Printprint("Hello, World!")
    Comment// single line /* multi */

    🎉 Lesson Complete!

    You've learned what Swift is and written your first program! Next, we'll dive deep into variables and data types.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service