O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Workshop iOS 2: Swift - Structures

Workshop Swift Structs, Enums and Classes:
-Structs & Classes
-Initialization
-Access Levels & Extensions
-Enums & associated values

Presentado por los ingenieros Jordi Serra y Pierluigi Cifani

  • Entre para ver os comentários

  • Seja a primeira pessoa a gostar disto

Workshop iOS 2: Swift - Structures

  1. 1. Swift: Structures Jordi Serra – Pierluigi Cifani iOS Workshops
  2. 2. Overview - Structs & Classes - Initialization - Access Levels & Extensions - Enums & associated values
  3. 3. Structs and Classes
  4. 4. Structs and Classes Structs and Classes are the main structures in Swift Basic syntax:
  5. 5. Comparing Classes and Structs Both can: - Define properties & methods - Define initializers to set up their initial state - Be extended through extension syntax - Conform to protocols Classes have additional capabilities: - Inheritance - Type casting - Deinitializer - More than one reference to a class instance
  6. 6. Comparing Classes and Structs Structs are passed by value, Classes are by reference - Structs are non mutating by default - Structs are copied whenever passed into a function (optimization with copy on write) When to use struct over class: - Encapsulates few data types - It can be copied around (no struct singletons) - Values inside are structs themselves - It does not need to inherit behavior Struct examples: Vector, Points, any Models and ViewModel
  7. 7. Initialization & Properties
  8. 8. Initialization Special function with init keyword - Accepts params, never returns - All non-optional values must be set in initializer - The compiler sets default initializer if all props have default values - Can be failable: init?() - And a lot more
  9. 9. Properties Properties in structs and classes hold values than can change or not over time. Declared using either var or let
  10. 10. Singleton Singletons in swift are very straightforward
  11. 11. Getters and Setters Properties can have its own get & set functions .. and property observers
  12. 12. Access types & extensions
  13. 13. Access Types (I) Access Levels (from less to more restrictive) open, public, internal, fileprivate and private Open: Enables usage, inheritance and override functionality from anywhere in the module and any other module that imports it Public: Enables usage, from anywhere in/out the module, enables inheritance and override from the same module only.
  14. 14. Access Types (II) Internal: Enables usage from anywhere inside the module, but not outside the module Fileprivate: Restricts the use of an entity to its own defining source file Private: Restricts the use of an entity to the enclosing declaration
  15. 15. Extensions Used to add new functionality to classes and structs Extensions in Swift can: - Add computed instance properties and computed type properties - Define instance methods and type methods - Provide new initializers - Define and use new nested types - Make an existing type conform to a protocol - Provide default protocol implementations Extensions allow extending types for which you do not have access to the original source code
  16. 16. Extensions Syntax Computed Property
  17. 17. Extensions Syntax Initializers Methods
  18. 18. Mutating Functions Structs are always passed as values. Therefore, it can never be changed inside a function (they are always a copy) To mutate a struct, use mutating keyword
  19. 19. Enums & associated values
  20. 20. Enums Declaration Usage
  21. 21. Enums Enums can be extended the same way as any other struct
  22. 22. Raw Values Enum declaration can extend a raw value type. Then, it has rawValue property available
  23. 23. Associated Values Enum cases can have a type associated. This should be initialized whenever the enum is.
  24. 24. Associated Values (II) The associated values can be taken inside the switch statement, as a let or var parameter

×