Category: 01. Basic

https://cdn3d.iconscout.com/3d/premium/thumb/book-3d-icon-png-download-8786672.png

  • Swift Arrays

    Swift Arrays

    Arrays store ordered collections of values of the same type.

    Arrays are value types and copy on write.


    Create and Access

    Create arrays with literals or initializers.

    Access elements by index and use properties like count and isEmpty.

    Example

    var numbers = [10, 20, 30]
    print(numbers[0])         // 10
    numbers.append(40)
    print(numbers.count)      // 4
    print(numbers.isEmpty)    // false

    This example creates an array, accesses items, appends, and inspects properties.

    Tip: Arrays are value types.

    Mutating a copy will not affect the original (copy-on-write semantics).



    Insert and Remove

    Modify arrays by inserting and removing elements.

    Example

    var items = ["A", "B", "C"]
    items.insert("X", at: 1)
    print(items)
    items.remove(at: 2)
    print(items)
  • Unicode & Scalars

    Swift strings are Unicode-correct.

    A single character can be composed of multiple Unicode scalars.

    Visually identical strings can compare equal.


    Composed Characters

    Some characters are made from multiple Unicode scalars; Swift treats them as a single character for comparison and length.

    Example

    let e1 = "é"
    let e2 = "e\u{301}"   // e + COMBINING ACUTE ACCENT
    print(e1 == e2)
    print(e2)

    This example shows that a precomposed character and a composed sequence are considered equal in Swift.



    Unicode Scalars

    Inspect the underlying Unicode scalar values with the unicodeScalars view.

    The unicodeScalars view yields values of type UnicodeScalar.

    Example

    let s = "e\u{301}"
    for scalar in s.unicodeScalars {
      print(scalar.value)   // 101, 769
    }
  • Swift Strings: Special Characters

    Use escape sequences inside strings: \n (newline), \t (tab), \" (quote), \\ (backslash).


    Escapes

    Escape special characters with backslashes to represent newlines, tabs, quotes, or backslashes themselves.

    Example

    print("Hello\nSwift")
    print("A\tB\tC")
    print("\"quoted\"")
    print("\\")


    Multiline Strings

    Use triple quotes """ for multiline string literals. Indentation before closing quotes is ignored.

    Example

    let poem = """
    Roses are red,
    Violets are blue.
    """
    print(poem)
  • Numbers and Strings

    Swift Strings: Numbers and Strings

    Use string interpolation to mix numbers with text.

    Convert numbers to strings explicitly with String(value) when concatenating.


    Mix Text and Numbers

    Use interpolation to embed numbers directly in strings, or convert numbers with String(value) when concatenating.

    Example

    let age = 5
    print("Age: \(age)")          // interpolation
    let text = "You are " + String(age)
    print(text)
    let pi = 3.14
    print("pi = \(pi)")

    This example shows interpolation and explicit conversion with String().



    Convert String to Number

    Use numeric initializers like Int("123") which return optionals because conversion can fail.

    Example

    let s1 = "123"
    let n1 = Int(s1)           // Int?
    print(n1 ?? 0)
    
    let s2 = "abc"
    let n2 = Int(s2)           // nil (not a number)
    print(n2 == nil)
  • Swift Strings: Concatenation

    Join strings with +, or use interpolation to insert values into a string.


    Concatenate vs Interpolate

    Use + to make a new string. Use interpolation to insert values inline.

    Example

    let first = "Hello"
    let second = "Swift"
    // Concatenation
    print(first + " " + second)
    // Interpolation
    print("\(first), \(second)!")

    Interpolation is often clearer when mixing text and values.



    Append Strings

    Use += to append to a mutable string.

    Example

    var s = "Hello"
    s += ", Swift"
    print(s)
  • Swift Strings

    Strings are text. Use + to join, interpolation with \(value) to insert values, and properties like count and isEmpty to inspect them.


    Basics

    Create, combine, and inspect strings with concatenation, interpolation, and common properties.

    Example

    let s1 = "Hello"
    let s2 = "Swift"
    print(s1 + " " + s2)
    print("\(s1), \(s2)!")
    let word = "Swift"
    print(word.count)
    print(s1.isEmpty)

    This example shows concatenation, interpolation, count, and isEmpty.



    Substring and Case

    Get substrings with indices. Uppercased/lowercased creates new strings without changing the original.

    Example

    let text = "Swift"
    let start = text.startIndex
    let end = text.index(start, offsetBy: 3)
    let sub = text[start..<end]  // "Swi"
    print(sub)
    print(text.uppercased())
  • Swift Operator Precedence

    Swift Operator Precedence

    Operator precedence determines evaluation order.

    Multiplication comes before addition; && before ||.

    Use parentheses to control order.


    Control order of evaluation with precedence

    Understand how operators are prioritized and add parentheses to make evaluation order explicit when needed.

    Example

    print(2 + 3 * 4)
    print((2 + 3) * 4)
    print(true || false && false)
    print((true || false) && false)

    This example shows how parentheses change the result.



    Boolean Precedence

    && is evaluated before ||. Use parentheses to clarify intention.

    Example

    let a = true
    let b = false
    let c = true
    
    print(a || b && c)         // true (&& before ||)
    print((a || b) && c)       // true
    print(a && b || c)         // true ((a && b) || c)
  • Swift Logical Operators

    Swift Logical Operators

    Combine booleans with && (AND), || (OR), and ! (NOT).


    Logical AND (&&)

    Both must be true for the result to be true.

    Example

    let isOwner = true
    let isAdmin = false
    print(isOwner && isAdmin)
    print(true && true)

    Logical OR (||)

    At least one true makes the result true.

    Example

    let a = true
    let b = false
    print(a || b)
    print(false || false)


    Logical NOT (!)

    Flip a boolean value: true becomes false and vice versa.

    Example

    let flag = false
    print(!flag)
    print(!true)

  • Swift Comparison Operators

    Swift Comparison Operators

    Use comparison operators to compare values: ==!=><>=<=.

    They return Bool.


    Compare Integers

    Use ==!=><>=, and <= to compare numeric values.

    The result is a Bool.

    Example

    let a = 5, b = 2
    print(a == b)
    print(a != b)
    print(a > b)
    print(a < b)
    print(a >= 5)

    This example prints the result of several comparisons.



    Compare Strings

    Strings compare lexicographically (dictionary order).

    Comparison is case-sensitive.

    Example

    print("apple" < "banana")  // true
    print("Swift" == "Swift")  // true
    print("cat" > "car")       // true

    This example compares strings using the same operators.

  • Swift Assignment Operators

    Swift Assignment Operators

    Use = to assign a value.

    Compound assignment operators like +=-=*= and /= update a variable in place.


    Compound Assignment

    Use compound operators like += and *= to update a variable in place.

    Example

    var total = 10
    total += 5
    print(total)
    total -= 3
    print(total)
    total *= 2
    print(total)
    total /= 4
    print(total)

    This example shows updating a variable with compound assignment operators.



    Append to String

    You can also use += with strings to append text to a mutable string variable.

    Example

    var s = "Hello"
    s += ", Swift"
    print(s)

    This example appends to a string in place with +=.