Workshop iOS 2: Swift - Structures

Preview:

Citation preview

Swift: StructuresJordi 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 SwiftBasic 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 initkeyword- 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 itPublic: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 moduleFileprivate:Restricts the use of an entity to its own defining source filePrivate:Restricts the use of an entity to the enclosing declaration

Extensions

Used to add new functionality to classes and structsExtensions 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