• Data Types

    • Character, String, Int, Float, Double, Bool
  • Complex Data Types

    • Arrays/Dictionaries/Sets/Enumerations/Tuples/Optionals/Structs/Classes/Closures
  • Value Types & Reference Type

    • (def) All Types in Swift are one of the two.
    • Value Types:
      • (def) A type that creates a new instance (copy) when assigned to a variable or constant, or when passed to a function.
      • (e.g.) Structs/Enums/Tuples/Int/Double/String/Array/Dictionary/Set
    • Reference Type:
      • (def) A type that once initialized, when assigned to a variable or constant, or when passed to a function, returns a reference to the same existing instance.
      • Once instantiated, when we either assign it or pass it as a value, we are actually assigning or passing around the reference to the original instance (i.e. its location in memory).
      • (e.g.) Class/Function/Closure
  • String Interpolation

    • (def) a way to construct a string from constants, variables, literals, and expressions.
    print("Kobe \\("Bryant")")
    print("Number \\(24)")
    print("Numbers combined: \\(24 + 8)")
    print("Some function: \\(kobe())")
    print("Some ternary operator: \\(mvp == "kobe" ? "legend" : "meh")")
    
  • Subscripts

    • (def) Shortcut that enable you to query instances of a type (arrays/sets/dictionaries) by writing one more more values in square brackets after the instance name.
      • *Subscripts for dictionaries returns optional types.
        • Use subscript for dictionary with no value for the key used → returns nil.
        • Use subscript for array with no value inside → outputs error.
    • Code:
    /* Suscripting an array */
    var ballBrothers = ["Lonzo", "Gelo", "Melo"]
    print(ballBrothers[1]) // output: Gelo
    
    -----------------------------------------------------------------------
    /* Subscripting a dictionary */
    var ballBrothers = ["Lonzo": "Pelicans", "Gelo": "Swarm", "Melo": "Hornets"]
    print(ballBrothers["Melo"]) // output: Hornets
    
  • Arrays

    var nba = ["Kobe", "Lebron", "Stephen", "Lavar"]
    
    // Initalizing an empty array (Type Annotation)
    var scores = Array<Int>()
    //or
    var scores = [Int]()
    // or
    var scores: [Int] = []
    
    //To append
    nba.append("Shaq")
    
    //To count # of data
    nba.count
    
    //To remove a data
    nba.remove(at: 3)
    
    //To remove all
    nba.removeAll()
    
    //To check if array contains specific data
    nba.contains("shaq")
    
    // To sort alphabetically/numerically
    nba.sorted()
    
    //To sort in reverse order
    nba.reverse()
    
  • Dictionaries

    • (def) uses a key-value format
    • Mutable. Entries can be added, removed, changed.
    • Rather than trying to remember that array index 2 means a player’s number, we could just write user["number"] – it’s much more convenient.
    let nba = [
    "name" = "Kobe",
    "position" = "SG",
    "number" = "24"
    ]
    
    //To initialize empty dictionary (Type Annotation)
    let mlb = [String: String]()
    
    //To append
    nba["height"] = "198"
    
    //To fetch data
    nba["name"] //Throws a warning without a 'default' value
    nba["name", default: "Unknown"] //No warnings
    /* This is incase there is no value attached to a key,
    		not that it always should. */
    
    
  • Tuples

    • (def) a group of different values enclosed in parentheses. Each value inside a tuple can be of different data types.
    • Ordered. Duplicates allowed.
    • Fixed in size. Mutable, but values/data types can’t be added/removed. (If assigned to a var, that is. Immutable if on let.)
    • You can also give element names to each values: var kobe = (name: “Kobe”, number: 24)
    • Values inside can be accessed using dot notation with element names or indice.
    • Tuple is effectively just a Struct without a name.
      • Use tuples when you want to return two or more arbitrary pieces of values from a function. Structs when you have some fixed data you want to send or receive multiple times.
    var kobe = ("Kobe", 24)
    // or
    var kobe = (name: "Kobe", number: 24)
    
    print("Name: ", kobe.0)
    print("Number", kobe.1)
    // or
    print("Name: ", kobe.name)
    print("Number", kobe.number)
    
    /*function that returns a Tuple*/
    func mvpElementName() -> (name: String, number: Int) {
    	("Kobe", 24)
    }
    //or
    func mvpElementName() -> (name: String, number: Int) {
    	return (name: "Kobe", number: 24)
    }
    //or
    func mvpIndices -> (String, Int) {
    	("Kobe", 24)
    }
    
    var mvp2007_elementName = mvpElementName()
    print(mvp2007_elementName) //(name: "Kobe", number: 24)
    var mvp2007_indices = mvpIndices()
    print(print2007_indices) //("Kobe", 24)
    
    /*function that takes an empty Tuple as a parameter*/
    func someFunction(someTuple: ()) {
    	//some code
    }
    
    /*Assigning individual Tuple values into individual variables*/
    func mvp() -> (firstName: String, jerseyNumber: Int) {
    	("Kobe", 24)
    }
    
    //option 1
    var mvp2007 = mvp()
    
    var name = mvp2007.firstName
    var number = mvp2007.jerseyNumber
    print("His name is \\(name), number \\(number)") //His name is Kobe, number 24
    																							 // \\(name) is cleaner than \\(mvp2007.firstName)
    
    //option 2 ("Deconstructuring":)
    var (name, number) = mvp()
    print("His name is \\(name), number \\(number)") //His name is Kobe, number 24
    																							 // even cleaner
    
  • Sets

    • Unordered. No duplicates. Of the same type.
      • Data is stored in highly optimized way (partly) for this reason. Much faster to locate data.
      • Array: Swift traverses to find data. Sets: Swift uses a hash function to find data with a hash value assigned to it. (hence the speed)
    • Additional data cannot be appended. But can be inserted. ("append" typically refers to adding an element to the end of an ordered collection)
    var players = Set(["Kobe", "Lebron", "Shaq", "Yao Ming"])
    
    //Initializing an empty set (Type Annotation)
    var players = Set<String>()
    
    //To ~~append~~ insert
    players.insert("Lavar")
    
    //To see if Set contains data
    players.contains("Kobe")
    
    //To count # of data
    players.count
    
    //To sort data alphabetically/numerically (returns a sorted **array**)
    players.sorted()