Sequence, Collection and Array
in Swift
Pofat @ COSCUP 2020
Slide 2
Slide 2 text
Hi, I am Pofat
Podcaster @ [weak self]
@weak_self
iOS Developer
@PofatTseng
pofat
Slide 3
Slide 3 text
Swift
Slide 4
Slide 4 text
About Swift
• Goal: Safe, fast and expressive with modern approach
• Apache 2.0 license with a Runtime Library Exception
• Swift project is governed by a core team that drives the strategic direction
by working with the community.
Slide 5
Slide 5 text
Swift Projects
• Swift compiler: https://swift.org/compiler-stdlib/
• The standard library bundled as part of the language
• Core libraries that provide higher-level functionality: https://swift.org/core-
libraries/
• The LLDB Debugger with Swift REPL: https://swift.org/lldb/
• Swift Package Manager: https://swift.org/package-manager/
• Xcode playground support
Slide 6
Slide 6 text
Platform Support
• Apple: iOS, macOS, watchOS, iPadOS and tvOS
• Linux: Ubuntu, CentOS, Amazon Linux2 and etc..
Slide 7
Slide 7 text
Swift Evolution
• Manifesto: Describe the proposed roadmap and goal for major projects.
• Proposal: Describe the design proposal for new features or changes
• Status of proposals can be traces at https://apple.github.io/swift-evolution/
Slide 8
Slide 8 text
Build Swift!
Slide 9
Slide 9 text
Requirement
• Ninja/cmake
• Xcode
• Power
• Env: Swift 5.2.4-RELEASE
VS Code Setup for Swift
Install Xcode
• Install Xcode (The correct way to install Xcode)
Slide 15
Slide 15 text
VS Code Setup for Swift
Install VS Code and NPM
• Install VS Code
• Install VS Code command line tool
• Install node and npm
Slide 16
Slide 16 text
VS Code Setup for Swift
Build and install SourceKit-LSP for VS Code
Slide 17
Slide 17 text
Happy Tracing
Slide 18
Slide 18 text
Debug Swift
Using VS Code
• Open cloned Swift source code root folder
• Create configuration launch.json
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
Standard Library
Slide 21
Slide 21 text
Fundamental Architecture
Swift
Standard library
Foundation
Slide 22
Slide 22 text
Standard Library
Core
• Standard library: Core, Runtime and SDK overlays
• Definitions of all data types, protocol, functions, etc.
• All written in Swift
• Use GYB for repetitive codes
Compile Swift
Swift
Frontend
IRGen LLVM
Swift code SIL LLVM IR Machine code
Slide 27
Slide 27 text
BuiltIn
BuiltIn
LLVM IR
Standard
Library
Type
Method
BuiltIn.Type
BuiltIn.Method
Slide 28
Slide 28 text
Attributes
• @inlinable : Make it inline when compile with -O
• @_transparent : Must be inline even when compile with -None
• @usableFromInline : Temporary change scope during inline stage
• @_fixed_layout : Processed at SIL stage. It tells compiler that access
properties by offset is possible without looking up metadata.
• @frozen : Only allowed in library evolution mode. Marked enum or struct
can’t change their declaration by adding, removing, or reordering an
enumeration’s cases or a structure’s stored instance properties.
Slide 29
Slide 29 text
Example: Bool
Slide 30
Slide 30 text
Protocol-Oriented Programming
Slide 31
Slide 31 text
Why Protocol?
• Contract of interfaces
• Cross-cutting concern
• Better way to share code than inheritance
Slide 32
Slide 32 text
Protocol
Hashable.swift
Slide 33
Slide 33 text
Protocols in Standard Library
• Identity: Sequence, Collection, …
• Ability: Hashable, Equatable, …
• Able to convert to: CustomStringConvertible, …
Slide 34
Slide 34 text
Sequence
Slide 35
Slide 35 text
Sequence
• Definition: A sequence is a list of values that you can step through one at a
time.
• Iterator step through the whole sequence
_SmallString
SmallString.swift
RawBitPattern.0 RawBitPattern.1
0 1 2 3 4 5 6 7 8 9 A B C D E F
Discriminator
UTF8 Code Point
56 57 58 59 60 61 62 63
Used Count isForeign
0
isSmall
1
ASCII
0/1
isImmortal
1
Slide 77
Slide 77 text
String Initialize
Small String
String _StringGuts _StringObject _SmallString
Raw bytes after transcode
Init with buffer
Return smallString
smallString
Init with smallString
Init with _StringObject
Return _StringObject
Return _StringGuts
Init with guts