workshop ios 2: swift - structures

25
Swift: Structures Jordi Serra – Pierluigi Cifani iOS Workshops

Upload: visual-engineering

Post on 14-Jan-2017

136 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Workshop iOS 2: Swift - Structures

Swift: StructuresJordi Serra – Pierluigi Cifani

iOS Workshops

Page 2: Workshop iOS 2: Swift - Structures

Overview

- Structs & Classes- Initialization- Access Levels & Extensions- Enums & associated values

Page 3: Workshop iOS 2: Swift - Structures

Structs and Classes

Page 4: Workshop iOS 2: Swift - Structures

Structs and Classes

Structs and Classes are the main structures in SwiftBasic syntax:

Page 5: Workshop iOS 2: Swift - Structures

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

Page 6: Workshop iOS 2: Swift - Structures

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

Page 7: Workshop iOS 2: Swift - Structures

Initialization & Properties

Page 8: Workshop iOS 2: Swift - Structures

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

Page 9: Workshop iOS 2: Swift - Structures

Properties

Properties in structs and classes hold values than can change or not over time.Declared using either var or let

Page 10: Workshop iOS 2: Swift - Structures

Singleton

Singletons in swift are very straightforward

Page 11: Workshop iOS 2: Swift - Structures

Getters and Setters

Properties can have its own get & set functions

.. and property observers

Page 12: Workshop iOS 2: Swift - Structures

Access types & extensions

Page 13: Workshop iOS 2: Swift - Structures

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.

Page 14: Workshop iOS 2: Swift - Structures

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

Page 15: Workshop iOS 2: Swift - Structures

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

Page 16: Workshop iOS 2: Swift - Structures

Extensions Syntax

Computed Property

Page 17: Workshop iOS 2: Swift - Structures

Extensions Syntax

Initializers

Methods

Page 18: Workshop iOS 2: Swift - Structures

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

Page 19: Workshop iOS 2: Swift - Structures

Enums & associated values

Page 20: Workshop iOS 2: Swift - Structures

Enums

Declaration Usage

Page 21: Workshop iOS 2: Swift - Structures

Enums

Enums can be extended the same way as any other struct

Page 22: Workshop iOS 2: Swift - Structures

Raw Values

Enum declaration can extend a raw value type. Then, it has rawValue property available

Page 23: Workshop iOS 2: Swift - Structures

Associated Values

Enum cases can have a type associated. This should be initialized whenever the enum is.

Page 24: Workshop iOS 2: Swift - Structures

Associated Values (II)

The associated values can be taken inside the switch statement, as a let or var parameter

Page 25: Workshop iOS 2: Swift - Structures