Types A type is a classification of data which tells the computer how the programmer intends to use that data. — Wikipedia So basically programmers tell the computer their intentions and the computer will complain if the programmer fails to adhere to them.
Enums An enum is a type with mutually-exclusive cases. enum ArtworkAvailability { case notForSale case forSale case onHold case sold } ArtworkAvailability.forSale
Variables Variables are a place to store data and refer to it by name. Variables in Swift are defined one of two ways: —var variables whose values can change. —let constants whose values can never change. Swift generally prefers let. If you use var when you don't need it, the compiler will suggest using let instead.
Type Inference Swift is a strongly-typed language, but compare these two equivalent lines of code: let array: [String] = ["Ash", "Orta", "Sarah"] ... let array = ["Ash", "Orta", "Sarah"] In either case, Swift needs to disambiguate what is stored in the array.
Named Tuples Swift also supports named tuples to access elements at specific positions in a tuple. let presenter = (name: "Ash", pets: [Animal.!, .!]) presenter.name // "Ash" presenter.pets // [Animal.!, .!]
Classes Classes contain data and are passed by reference. class Person { let name: String let dateOfBirth: Date ... } let person = Person(name: "Ash", dateOfBirth: ...
Structs Structs contain data and are passed by value. struct Person { let name: String let dateOfBirth: Date ... } let person = Person(name: "Ash", dateOfBirth: ...
Structs vs Classes —Pass by value vs. by reference. —Classes can extend other classes for OOP. —Structs have no hierarchy, cannot extend other structs.
Protocols Protocols are for loosely-coupled contracts. In other languages, they're sometimes called interfaces. They are a way to know that a type has some functions without knowing what that type is. Example: Table view data source.
Optionals Optionals are a way to define whether or not a value can be nil, or "missing". Optionals are signified by ? and are a type. So Int? is an optional integer because it might be nil. This is another improvement over Objective-C.
Extensions with Generics We can extend generic types with where clauses so the extension only exists when that clause is true. For example, if we want to extend arrays of things that conform to Occupiable: extension Array where Element: Occupiable { var isFilledWithEmpties: Bool { return self.filter({ $0.isNotEmpty }).count > 0 } }
Wrap-up 1. Swift was a strategic replacement for Objective-C 2. Open sourcing Swift has been good for the community 3. Swift syntax supports a variety of paradigms