Swift Notes for Professionals

Programming · notes

Voir tous les documents en programmation

Swift

Notes for Professionals

Swift"

Notes for Professionals

200+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an uno$cial free book created for educational purposes and is

not a$liated with o$cial Swift" group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

...................................................................................................................................................................................

1

Chapter 1: Getting started with Swift Language

..........................................................................................

2

............................................................................................................................

2

Section 1.1: Your rst Swift program

Section 1.2: Your rst program in Swift on a Mac (using a Playground)

Section 1.3: Your rst program in Swift Playgrounds app on iPad

Section 1.4: Installing Swift

Section 1.5: Optional Value and Optional enum

............................................................................................................................................

.........................................................................................................

.................................................................

3

...........................................................................

Chapter 2: Variables & Properties

Section 2.1: Creating a Variable

Section 2.2: Property Observers

Section 2.3: Lazy Stored Properties

Section 2.4: Property Basics

Section 2.5: Computed Properties

Section 2.6: Local and Global Variables

Section 2.7: Type Properties

.....................................................................................................................

.................................................................................................................................

................................................................................................................................

...........................................................................................................................

.......................................................................................................................................

.............................................................................................................................

...................................................................................................................

12

.......................................................................................................................................

13

Chapter 3: Numbers

..................................................................................................................................................

14

......................................................................................................................

........................................................................................................

Section 3.1: Number types and literals

Section 3.2: Convert numbers to/from strings

Section 3.3: Rounding

Section 3.4: Random number generation

Section 3.5: Convert one numeric type to another

Section 3.6: Exponentiation

..................................................................................................................................................

.................................................................................................................

..................................................................................................

17

........................................................................................................................................

...................................................................................................................

.....................................................................................................................

...............................................................................................................................

....................................................................................................

..............................................................................................................

....................................................................................................................................

....................................................................

........................................................................................................................................

..............................................................................................................

....................................................................................................................................................

..........................................................................................

........................................................................................

................................................................................................................................

......................................................................................................

Chapter 4: Strings and Characters

Section 4.1: String & Character Literals

Section 4.2: Concatenate strings

Section 4.3: String Encoding and Decomposition

Section 4.4: Examine and compare strings

Section 4.5: Reversing Strings

Section 4.6: Check if String contains Characters from a Dened Set

Section 4.7: String Iteration

Section 4.8: Splitting a String into an Array

Section 4.9: Unicode

Section 4.10: Converting Swift string to a number type

Section 4.11: Convert String to and from Data / NSData

Section 4.12: Formatting Strings

Section 4.13: Uppercase and Lowercase Strings

Section 4.14: Remove characters from a string not dened in Set

Section 4.15: Count occurrences of a Character into a String

Section 4.16: Remove leading and trailing WhiteSpace and NewLine

........................................................................

................................................................................

...................................................................

27

Chapter 5: Booleans

..................................................................................................................................................

29

...........................................................................................................................................

Section 5.1: What is Bool?

Section 5.2: Booleans and Inline Conditionals

Section 5.3: Boolean Logical Operators

Section 5.4: Negate a Bool with the prex ! operator

..........................................................................................................

....................................................................................................................

..............................................................................................

30

Chapter 6: Arrays

.......................................................................................................................................................

31

Section 6.1: Basics of Arrays

Section 6.2: Extracting values of a given type from an Array with atMap(_:)

.......................................................................................................................................

...................................................

32

31

7

8

8

10

10

10

11

11

12

14

Publicité

15

15

16

17

18

18

19

20

20

21

21

22

24

24

25

25

26

26

27

27

29

29

30

.....................................................................................................

...........................................................

.................................................................

32

...................................................

33

...................................................................

.......................................................

Section 6.3: Combining an Array's elements with reduce(_:combine:)

Section 6.4: Flattening the result of an Array transformation with atMap(_:)

Section 6.5: Lazily attening a multidimensional Array with atten()

Section 6.6: Filtering out nil from an Array transformation with atMap(_:)

Section 6.7: Subscripting an Array with a Range

Section 6.8: Removing element from an array without knowing it's index

Section 6.9: Sorting an Array of Strings

Section 6.10: Accessing indices safely

Section 6.11: Filtering an Array

Section 6.12: Transforming the elements of an Array with map(_:)

Section 6.13: Useful Methods

Section 6.14: Sorting an Array

Section 6.15: Finding the minimum or maximum element of an Array

Section 6.16: Modifying values in an array

Section 6.17: Comparing 2 Arrays with zip

Section 6.18: Grouping Array values

Section 6.19: Value Semantics

Section 6.20: Accessing Array Values

....................................................................................................................

.......................................................................................................................

...................................................................................................................................

......................................................................

......................................................................................................................................

....................................................................................................................................

.................................................................

...............................................................................................................

................................................................................................................

..........................................................................................................................

....................................................................................................................................

.......................................................................................................................

Chapter 7: Tuples

........................................................................................................................................................

44

....................................................................................................................................

Section 7.1: What are Tuples?

Section 7.2: Decomposing into individual variables

Section 7.3: Tuples as the Return Value of Functions

Section 7.4: Using a typealias to name your tuple type

Section 7.5: Swapping values

Section 7.6: Tuples as Case in Switch

................................................................................................

.............................................................................................

.........................................................................................

45

.....................................................................................................................................

........................................................................................................................

Chapter 8: Enums

.......................................................................................................................................................

48

Section 8.1: Basic enumerations

Section 8.2: Enums with associated values

Section 8.3: Indirect payloads

Section 8.4: Raw and Hash values

Section 8.5: Initializers

Section 8.6: Enumerations share many features with classes and structures

Section 8.7: Nested Enumerations

.................................................................................................................................

..............................................................................................................

....................................................................................................................................

............................................................................................................................

.................................................................................................................................................

.............................................................................................................................

53

.....................................................

52

Chapter 9: Structs

......................................................................................................................................................

54

Section 9.1: Structs are value types

Section 9.2: Accessing members of struct

Section 9.3: Basics of Structs

Section 9.4: Mutating a Struct

Section 9.5: Structs cannot inherit

...........................................................................................................................

54

................................................................................................................

54

.....................................................................................................................................

....................................................................................................................................

.............................................................................................................................

Chapter 10: Sets

...........................................................................................................................................................

57

Section 10.1: Declaring Sets

Section 10.2: Performing operations on sets

Section 10.3: CountedSet

Section 10.4: Modifying values in a set

Section 10.5: Checking whether a set contains a value

Section 10.6: Adding values of my own type to a Set

........................................................................................................................................

............................................................................................................

.............................................................................................................................................

......................................................................................................................

...........................................................................................

58

..............................................................................................

58

Chapter 11: Dictionaries

...........................................................................................................................................

60

Section 11.1: Declaring Dictionaries

Publicité

Section 11.2: Accessing Values

Section 11.3: Change Value of Dictionary using Key

............................................................................................................................

....................................................................................................................................

................................................................................................

61

33

34

34

35

35

36

37

37

38

38

39

40

40

41

42

42

44

44

45

46

46

48

48

49

50

51

54

55

55

57

57

58

58

60

60

Section 11.4: Get all keys in Dictionary

Section 11.5: Modifying Dictionaries

Section 11.6: Merge two dictionaries

.......................................................................................................................

61

...........................................................................................................................

..........................................................................................................................

Chapter 12: Switch

......................................................................................................................................................

63

.............................................................................................................................

................................................................................................................................................

Section 12.1: Switch and Optionals

Section 12.2: Basic Use

Section 12.3: Matching a Range

Section 12.4: Partial matching

Section 12.5: Using the where statement in a switch

Section 12.6: Matching Multiple Values

Section 12.7: Switch and Enums

Section 12.8: Switches and tuples

Section 12.9: Satisfy one of multiple constraints using switch

Section 12.10: Matching based on class - great for prepareForSegue

Section 12.11: Switch fallthroughs

.................................................................................................................................

....................................................................................................................................

...............................................................................................

......................................................................................................................

.................................................................................................................................

..............................................................................................................................

................................................................................

..................................................................

67

...............................................................................................................................

68

Chapter 13: Optionals

................................................................................................................................................

69

Section 13.1: Types of Optionals

Section 13.2: Unwrapping an Optional

Section 13.3: Nil Coalescing Operator

Section 13.4: Optional Chaining

Section 13.5: Overview - Why Optionals?

.................................................................................................................................

......................................................................................................................

........................................................................................................................

..................................................................................................................................

..................................................................................................................

72

Chapter 14: Conditionals

.........................................................................................................................................

74

Section 14.1: Optional binding and "where" clauses

Section 14.2: Using Guard

Section 14.3: Basic conditionals: if-statements

Section 14.4: Ternary operator

Section 14.5: Nil-Coalescing Operator

...........................................................................................................................................

.........................................................................................................

...................................................................................................................................

.......................................................................................................................

.................................................................................................

74

Chapter 15: Error Handling

....................................................................................................................................

78

Section 15.1: Error handling basics

Section 15.2: Catching di#erent error types

Section 15.3: Catch and Switch Pattern for Explicit Error Handling

Section 15.4: Disabling Error Propagation

Section 15.5: Create custom Error with localized description

........................................................................

80

................................................................................................................

.................................................................................

.............................................................................................................................

.............................................................................................................

Chapter 16: Loops

.......................................................................................................................................................

83

Section 16.1: For-in loop

Section 16.2: Repeat-while loop

Section 16.3: For-in loop with ltering

Section 16.4: Sequence Type forEach block

Section 16.5: while loop

Section 16.6: Breaking a loop

...............................................................................................................................................

.................................................................................................................................

........................................................................................................................

.............................................................................................................

86

...............................................................................................................................................

Publicité

.....................................................................................................................................

Chapter 17: Protocols

................................................................................................................................................

88

.......................................................................................................................................

...................................................................................................................................

.............................................................................................................

.............................................................................................................................

Section 17.1: Protocol Basics

Section 17.2: Delegate pattern

Section 17.3: Associated type requirements

Section 17.4: Class-Only Protocols

Section 17.5: Protocol extension for a specic conforming class

Section 17.6: Using the RawRepresentable protocol (Extensible Enum)

Section 17.7: Implementing Hashable protocol

...........................................................................

...............................................................

94

........................................................................................................

95

Chapter 18: Functions

...............................................................................................................................................

97

61

62

63

63

63

64

65

65

66

66

67

69

69

71

71

75

75

76

77

78

79

81

81

83

85

85

86

87

88

90

91

93

94

Section 18.1: Basic Use

Section 18.2: Functions with Parameters

Section 18.3: Subscripts

Section 18.4: Methods

Section 18.5: Variadic Parameters

Section 18.6: Operators are Functions

Section 18.7: Passing and returning functions

Section 18.8: Function types

Section 18.9: Inout Parameters

Section 18.10: Throwing Errors

Section 18.11: Returning Values

Section 18.12: Trailing Closure Syntax

Section 18.13: Functions With Closures

.................................................................................................................................................

...................................................................................................................

...............................................................................................................................................

..................................................................................................................................................

...........................................................................................................................

....................................................................................................................

........................................................................................................

101

.....................................................................................................................................

................................................................................................................................

.................................................................................................................................

................................................................................................................................

.....................................................................................................................

....................................................................................................................

Chapter 19: Extensions

...........................................................................................................................................

105

Section 19.1: What are Extensions?

Section 19.2: Variables and functions

Section 19.3: Initializers in Extensions

Section 19.4: Subscripts

Section 19.5: Protocol extensions

Section 19.6: Restrictions

Section 19.7: What are extensions and when to use them

..........................................................................................................................

......................................................................................................................

......................................................................................................................

.............................................................................................................................................

.............................................................................................................................

..........................................................................................................................................

....................................................................................

107

Chapter 20: Classes

.................................................................................................................................................

109

Section 20.1: Dening a Class

Section 20.2: Properties and Methods

Section 20.3: Reference Semantics

Section 20.4: Classes and Multiple Inheritance

Section 20.5: deinit

..................................................................................................................................

....................................................................................................................

.........................................................................................................................

......................................................................................................

110

....................................................................................................................................................

......................................................................................................................................

.........................................................................................................................................

Chapter 21: Type Casting

Section 21.1: Downcasting

Section 21.2: Type casting in Swift Language

Section 21.3: Upcasting

Section 21.4: Example of using a downcast on a function parameter involving subclassing

Section 21.5: Casting with switch

.............................................................................................................................................

........................................................................................................

..............................................................................................................................

115

...........................

114

Chapter 22: Generics

...............................................................................................................................................

116

Publicité

Section 22.1: The Basics of Generics

Section 22.2: Constraining Generic Placeholder Types

Section 22.3: Generic Class Examples

Section 22.4: Using Generics to Simplify Array Functions

Section 22.5: Advanced Type Constraints

Section 22.6: Generic Class Inheritance

Section 22.7: Use generics to enhance type-safety

.......................................................................................................................

........................................................................................

.....................................................................................................................

....................................................................................

119

..............................................................................................................

..................................................................................................................

..............................................................................................

Chapter 23: OptionSet

............................................................................................................................................

122

Section 23.1: OptionSet Protocol

..............................................................................................................................

122

Chapter 24: Reading & Writing JSON

............................................................................................................

123

Section 24.1: JSON Serialization, Encoding, and Decoding with Apple Foundation and the Swift Standard

Library

................................................................................................................................................................

123

Section 24.2: SwiftyJSON

Section 24.3: Freddy

Section 24.4: JSON Parsing Swift 3

Section 24.5: Simple JSON parsing into custom objects

.........................................................................................................................................

.................................................................................................................................................

.........................................................................................................................

......................................................................................

131

97

97

98

99

100

100

101

101

101

102

102

103

105

105

106

106

106

107

109

109

109

111

112

112

112

114

116

117

118

119

120

121

126

127

129

Section 24.6: Arrow

...................................................................................................................................................

132

Chapter 25: Advanced Operators

Section 25.1: Bitwise Operators

Section 25.2: Custom Operators

Section 25.3: Overow Operators

Section 25.4: Commutative Operators

Section 25.5: Overloading + for Dictionaries

Section 25.6: Precedence of standard Swift operators

....................................................................................................................

................................................................................................................................

..............................................................................................................................

...........................................................................................................................

....................................................................................................................

..........................................................................................................

.........................................................................................

138

Chapter 26: Method Swizzling

............................................................................................................................

140

Section 26.1: Extending UIViewController and Swizzling viewDidLoad

Section 26.2: Basics of Swift Swizzling

Section 26.3: Basics of Swizzling - Objective-C

....................................................................................................................

......................................................................................................

................................................................

140

Chapter 27: Reection

...........................................................................................................................................

143

Section 27.1: Basic Usage for Mirror

Section 27.2: Getting type and names of properties for a class without having to instantiate it

........................................................................................................................

....................

143

143

Chapter 28: Access Control

.................................................................................................................................

147

Section 28.1: Basic Example using a Struct

Section 28.2: Subclassing Example

Section 28.3: Getters and Setters Example

.............................................................................................................

.........................................................................................................................

............................................................................................................

148

Chapter 29: Closures

...............................................................................................................................................

149

......................................................................................................................................

................................................................................................................................

.........................................................................................................

Section 29.1: Closure basics

Section 29.2: Syntax variations

Section 29.3: Passing closures into functions

Section 29.4: Captures, strong/weak references, and retain cycles

Section 29.5: Using closures for asynchronous coding

Section 29.6: Closures and Type Alias

...