KEMBAR78
Workshop iOS 2: Swift - Structures | PDF
Swift: Structures
Jordi Serra – Pierluigi Cifani
iOS Workshops
Overview
- Structs & Classes
- Initialization
- Access Levels & Extensions
- Enums & associated values
Structs and Classes
Structs and Classes
Structs and Classes are the main structures in Swift
Basic syntax:
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
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
Initialization & Properties
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
Properties
Properties in structs and classes hold values than can change
or not over time.
Declared using either var or let
Singleton
Singletons in swift are very straightforward
Getters and Setters
Properties can have its own get & set functions
.. and property observers
Access types & extensions
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.
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
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
Extensions Syntax
Computed Property
Extensions Syntax
Initializers
Methods
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
Enums & associated values
Enums
Declaration Usage
Enums
Enums can be extended the same way as any other struct
Raw Values
Enum declaration can extend a raw value type. Then, it has
rawValue property available
Associated Values
Enum cases can have a type associated. This should be
initialized whenever the enum is.
Associated Values (II)
The associated values can be taken inside the switch
statement, as a let or var parameter
Workshop iOS 2: Swift - Structures

Workshop iOS 2: Swift - Structures

  • 1.
    Swift: Structures Jordi Serra– Pierluigi Cifani iOS Workshops
  • 2.
    Overview - Structs &Classes - Initialization - Access Levels & Extensions - Enums & associated values
  • 3.
  • 4.
    Structs and Classes Structsand Classes are the main structures in Swift Basic syntax:
  • 5.
    Comparing Classes andStructs 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.
    Comparing Classes andStructs 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.
  • 8.
    Initialization Special function withinit 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.
    Properties Properties in structsand classes hold values than can change or not over time. Declared using either var or let
  • 10.
    Singleton Singletons in swiftare very straightforward
  • 11.
    Getters and Setters Propertiescan have its own get & set functions .. and property observers
  • 12.
    Access types &extensions
  • 13.
    Access Types (I) AccessLevels (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.
    Access Types (II) Internal: Enablesusage 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.
    Extensions Used to addnew 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.
  • 17.
  • 18.
    Mutating Functions Structs arealways 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.
  • 20.
  • 21.
    Enums Enums can beextended the same way as any other struct
  • 22.
    Raw Values Enum declarationcan extend a raw value type. Then, it has rawValue property available
  • 23.
    Associated Values Enum casescan have a type associated. This should be initialized whenever the enum is.
  • 24.
    Associated Values (II) Theassociated values can be taken inside the switch statement, as a let or var parameter