SlideShare a Scribd company logo
NSCoder Swift 
An introduction to Swift 
Andreas Blick - @aquarioverde 
October 18th, 2014
Swift
Swift 
• New programming language introduced by 
Apple on June 2014 at WWDC 
• Builds on the best of C and Objective-C, without 
the constraints of C compatibility 
• Development started by Chris Lattner, who also 
started the LLVM and Clang project
Swift 
• Safe programming patterns and modern 
features 
• Seamless access to all existing Cocoa 
frameworks 
• Mix-and-match interoperability with Objective-C
Playgrounds
Playgrounds 
• Tool / Feature for 
• learning 
• code development 
• experimentation
Playgrounds 
• Strings 
• Arrays & dictionaries 
• Color 
• Views 
• Images 
• … etc
var / let
Variables & Constants 
• Defined using var or let keyword 
• No need to define type 
• Swift can mostly infer the type 
• Can contain any unicode character 
var instrument: String = “guitar” 
let nrOfSongs = 42 
var trackLength = 3.23
Strings 
• Collection of characters that can be accessed easily 
• Seamlessly bridged: NSString API available 
• Mutability defined by assignment (var or let) 
• String interpolation for literals, variables and expressions 
let instrument = “guitar" 
! 
let music = "play (instrument)" 
let noise = "play " + instrument
Numbers 
• Integers are stored as Int or UInt 
• can be written as decimal, binary, octal, hexadecimal 
• have max and min properties: Int.max 
• Floating point numbers are stored as Float and Double 
• can be written as decimal and hexadecimal 
• Can contain underscores for readability: 7_777.77 
• Bool can be true or false
Arrays 
• Stores ordered list of multiple values 
• Arrays are type specific 
• Accessed using properties, methods or subscripting 
var numbers: Array<Int> = Array<Int>() 
var numbers: [Int] = [Int]() 
! 
numbers = [1,2,3] 
numbers = [Int](count:7,repeatedValue:0]
Dictionaries 
• Containers that store multiple key-value pairs of the 
same type 
• Accessed using properties, methods or subscripting 
• Assigning nil as a value removes it 
var numbers:Dictionary<Int,String> = Dictionary<Int,String>() 
! 
numbers = [1:“One”,2:“Two”,3:”Three”]
Tuples 
• Group multiple values of any type into a single compound value 
• Values can be of any type 
• Values can be accessed by index 
• Tuples can have named values 
let instrument = ("guitar", “strings") 
println(instrument.0) 
! 
let (name, family) = instrument 
! 
let instrument = (name: "guitar", family: "strings")
Loops 
• for-in loop to iterate using range 
• for-in loops to iterate over a collection 
• while / do-while loops if unknown number of iterations 
for ix in 1…5 { . . . } 
for var ix = 0; ix < 7; ix++ { . . . } 
for (key, value) in aDictionary { . . . } 
! 
while ix < 7 { . . . } 
do { . . . } while ix < 7
Conditionals 
• if / else for simple conditions 
• switch statements for complex conditions 
if number == 5 { . . . } 
! 
switch number { 
case 5: 
println(“5”) 
default: 
println(“default”) 
}
Conditionals - Switch 
• do not fall through (no break needed) 
• must be exhaustive (use default:) 
• support ranges: case 0…7: 
• support tuples: case (1, 2): 
• value binding: case (let x, 2): 
• can use where clause to check additional conditions 
case (let x, let y) where x == y:
Control Transfer Statements 
• continue 
• break 
• return 
• fallthrough 
• loops and switches can be labeled to be used with break and continue 
counter: for i in 1...7 { 
for j in 1...7 { 
println("(i) - (j)") 
if i == 3 { continue counter} 
} 
}
?
Optionals 
• Used where a value might be missing by adding a ? sign 
• Similar to nil in Objective-C, but can be used on any type 
• non optionals can not be nil 
• Optionals can implicitly be unwrapped by adding a ! sign (forced 
unwrapping) 
let instruments = ["guitar" : 6, "bass": 4, “ukulele”: “8”] 
let nrOfStrings: Int? = instruments[“guitar"] 
! 
if nrOfStrings != nil { 
println(nrOfStrings!) 
} else { . . . }
Optionals 
• Use optional binding to find out if an optional 
contains a value 
let instruments = ["guitar" : 6, "bass": 4, “ukulele”: “8”] 
let nrOfStrings: Int? = instruments[“guitar"] 
! 
if let nrOfStrings = instruments["guitar"] { 
println(nrOfStrings) 
} else { . . . } 
• Use optional chaining when a value might be nil 
let familyName = guitar.family?.name?
func
Functions 
• Functions are blocks of code that execute a specific task 
• Functions have parameters and can return values - default to void 
• Parameters can have default values 
• They should always be placed at the end of the list 
func play(instrument: String = "guitar") -> String { 
return "play (instrument)" 
} 
! 
play(instrument: "drums")
Functions 
• Parameter names are not required when calling a function 
that has no default values 
• Functions can return multiple values using tuples 
• Use # if external and internal parameter names are the same 
func play(instrument: String) -> (String, volume: Int) { 
return ("play (instrument)", 7) 
} 
play(“guitar").volume 
! 
func play(instrument instr: String) -> String { . . . } 
func play(#instrument: String) -> String { . . . }
Functions 
• Functions can take a variable number of 
arguments using variadic parameters 
• One function can only have one variadic 
parameter 
func playInstruments(instruments: String...) -> String { 
var play = "playing " 
for instrument in instruments { 
play += instrument + " " 
} 
return play 
} 
playInstruments("guitar", "drums", "sax")
Functions 
• Parameters are constants by default and can not be changed 
• They can be defined as variables using var 
• Variables parameters can only be modified within a function 
• To persist a variable outside a function the inout parameter is needed 
func swapTwoInts(inout a: Int, inout b: Int) { 
let tmpA = a 
a = b 
b = tmpA 
} 
var x = 3, y = 7 
swapTwoInts(&x, &y)
Functions 
• Functions have a specific function type that can be assigned to 
variables 
• They can be passed as parameters and returned by other 
functions 
• Functions can be nested 
func iPlay(player:(String) ->String, song:String) ->String { 
return player(song) 
} 
func playGuitar(song: String) -> String { 
return "playing (song) on guitar" 
} 
let playing = iPlay(playGuitar, "I sat by the ocean")
Closures 
{}
Closures 
• Closures are self-contained blocks of code that 
can be passed around 
• Functions are named closures! 
• Closures can capture and store references and 
constants from their surrounding context 
• Trailing closures can be used for readability 
• Closures start using the in keyword
Closures - Example 
let playMusic = { println("make some noise”) } 
! 
! 
! 
func repeat(count:Int, song:String, player:(String)->(String)) { 
for i in 0..<count { 
println(player(song)); 
} 
} 
repeat(5, "Even Flow") { 
(song: String) -> String in 
return "playing (song)" 
}
class
Classes 
• Classes are program code templates for creating 
objects, consisting of properties and methods 
• No need for interface/header files 
• No need to inherit from any other class 
• No abstract class support 
• Compare using the identity operator ===
Classes - Properties 
• Access to properties handled automatically 
• Properties do not have a corresponding instance 
variable 
• Every property needs to have a value assigned 
• Properties without a setter are read-only 
• Can have lazy stored properties (lazy) 
• Type properties are defined using class keyword
Classes - Example 
class Instrument { 
let type: String 
var isUsed: Bool = false 
init(type: String) { 
self.type = type 
} 
var info: String { 
get { 
return isUsed ? "used (type)" : "new (type)" 
} 
} 
} 
! 
let electricGuitar = Instrument(type: "guitar")
Classes - Properties 
• Property observers can respond to changes in a 
properties’ value (willSet & didSet) 
var isUsed: Bool = false { 
willSet(willBeUsed) { 
let msg = (self.isUsed ? "was" : "was not") + 
" used and “ + 
(willBeUsed ? "is used now" : "is not used now”) 
println(msg) 
} 
}
Classes - Methods 
• Methods are functions associated with a particular type 
• Subscripts are special methods (subscript) for 
accessing members. They can contain any number of 
input parameters of any type. 
class Instrument { 
func play() { 
println("make some noise") 
} 
subscript(ix: Int) -> String { 
return "model (ix)" 
} 
}
Initialization 
• The class needs to be completely initialized before 
accessing any property or method 
• Properties that are allowed to have no value can be 
declared optional using the ? sign 
• Property observers are NOT called on initialization 
• Every class must have at least one designated initialiser 
• Every class can have multiple convenience initializers 
• Deinitializers can be used for cleaning up
Initializer Chaining 
• Designated initializers must call a designated 
initializer from their immediate superclass 
• Convenience initializers must call another 
initializer available in the same class 
• Convenience initializers must ultimately end up 
calling a designated initializer
Initialization - Example 
class Instrument { 
let type: String 
var family: String? 
init(type: String) { 
self.type = type 
} 
convenience init(type: String, family: String) { 
self.init(type: type) 
self.family = family 
} 
deinit { 
println("instrument (type) destroyed") 
} 
} 
! 
let guitar = Instrument(type: “guitar")
Initialization 
• Closures can be used for initialising complex 
properties 
class Some { 
let some: String = { 
return "some" 
}() 
}
Inheritance 
• Classes can inherit methods, properties from other 
classes 
• Initializers are not inherited by default 
• Initialize own class properties before calling super.init 
• Overridden methods need override keyword 
• Methods marked as final can not be overridden 
• An entire class can be marked as final
Inheritance - Example 
class Guitar : Instrument { 
init() { 
super.init(type: "guitar") 
family = "Strings" 
} 
! 
override func play() { 
println("make some noise") 
} 
} 
! 
let guitar = Guitar()
struct
Structures 
• Structures are value types, they are always 
copied, not referenced 
• Structures can have initializers, methods and 
properties 
• Automatically generated memberwise initializer 
• Structures do not have deinitializers 
• Array and Dictionary are structures
Structures 
• All properties of constant structures are constant as 
well 
• Methods for modifying structure properties need 
the mutating keyword 
• Structures do not support inheritance 
• No type-casting 
• Type properties are defined using static keyword
Enumerations 
• An enumeration is a complete collection of items 
• Enumeration values are fully-fledged in their own 
• Enumerations are first-class types! 
• Enumerations can have methods & initialisers 
• Enumeration can have default raw values of the 
same type
Enumerations - Example 
enum Instrument: Int { 
case Guitar = 1, Base 
case Drums, Bongo, Trumpet, Saxophone 
func description() -> String { 
switch self { 
case .Guitar, .Base: 
return "Strings" 
case .Trumpet, .Saxophone: 
return "Wind" 
default: 
return "Unknown" 
} 
} 
} 
let instrument = Instrument.Trumpet 
instrument.description() 
instrument.toRaw() 
let unknown = Instrument.fromRaw(1)?.description()
Enumerations 
• Enumerations can have associated values 
enum Instrument { 
case Guitar(nrOfStrings: Int), Base 
case Drums, Bongo 
func description() -> String { 
switch self { 
case .Guitar(let nrOfStrings): 
return ("(nrOfStrings)-string guitar") 
default: 
return "Unknown" 
} 
} 
} 
let instrument = Instrument.Guitar(nrOfStrings: 6) 
instrument.description()
++
Extensions 
• Extensions add new functionality to existing classes, 
structures, … etc. 
They are similar to Categories in Objective-C 
• They can add methods, computed properties, initialisers 
but no stored properties 
extension Instrument { 
func rock() { 
println("(type) is rocking") 
} 
}
Protocols 
• A protocol defines methods and properties a class, struct 
or enumeration can or must adopt 
• Existing types can be extended to adopt a protocol 
• A protocol can inherit from one or more other protocols 
• Protocol composition can be used to combine protocols 
• Any protocol can be used as type 
• Define as @objc for type checking and optional support
Protocols - Example 
@objc protocol Instrument { 
func play() 
} 
! 
class Guitar: Instrument { 
func play() { 
println("playing guitar") 
} 
} 
! 
let items = [Guitar(), NSString()] 
! 
for item in items { 
if let instrument = item as? Instrument { 
instrument.play() 
} 
}
Generics 
• Generic code enables writing flexible, reusable 
functions and types 
func swapTwoValues<T>(inout a: T, inout b: T) { 
let tmpA = a 
a = b 
b = a 
}
Generics 
• You can define your own generic type 
• and specify type constraints 
struct Stack<T: Instrument> { 
var items = [T]() 
mutating func push(item: T) { 
items.append(item) 
} 
mutating func pop() { 
items.removeLast() 
} 
} 
var guitarStack = Stack<Guitar>()
References 
• WWDC 2014 Videos 
• Apple Books 
• The Swift Programming Language 
• Using Swift with Cocoa & Objective-C 
• Web 
• https://developer.apple.com/swift/
+++
ARC 
• Swift handles memory management using 
automatic reference counting 
• Think about relationship between object instead 
of memory 
• Avoid strong reference cycles by using weak or 
unowned references
Assertions 
• Use assertions whenever a condition has the 
potential to be false, but must be true 
• End code execution 
let age = -3 
assert(age >= 0, "age can no be less than 0")
Thank you! 
Andreas Blick - @aquarioverde

More Related Content

What's hot (20)

Python first day
Python first dayPython first day
Python first day
farkhand
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
mircodotta
 
Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
Kazunobu Tasaka
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
rivierarb
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
Kazunobu Tasaka
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed Overview
Buddha Tree
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
Oregon FIRST Robotics
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
Vasil Remeniuk
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
Nico Ludwig
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
fanf42
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
Nico Ludwig
 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception Handling
PranavSB
 
Swift as an OOP Language
Swift as an OOP LanguageSwift as an OOP Language
Swift as an OOP Language
Ahmed Ali
 
Practically Functional
Practically FunctionalPractically Functional
Practically Functional
djspiewak
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
OUM SAOKOSAL
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 
Intermediate JavaScript
Intermediate JavaScriptIntermediate JavaScript
Intermediate JavaScript
☆ Milan Adamovsky ☆
 
Python programming
Python programmingPython programming
Python programming
Ganesh Bhosale
 
Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
mircodotta
 
Javascript
JavascriptJavascript
Javascript
vikram singh
 
Python first day
Python first dayPython first day
Python first day
farkhand
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
mircodotta
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
rivierarb
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
Kazunobu Tasaka
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed Overview
Buddha Tree
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
Oregon FIRST Robotics
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
Vasil Remeniuk
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
Nico Ludwig
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
fanf42
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
Nico Ludwig
 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception Handling
PranavSB
 
Swift as an OOP Language
Swift as an OOP LanguageSwift as an OOP Language
Swift as an OOP Language
Ahmed Ali
 
Practically Functional
Practically FunctionalPractically Functional
Practically Functional
djspiewak
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 
Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)Effective Scala (SoftShake 2013)
Effective Scala (SoftShake 2013)
mircodotta
 

Similar to NSCoder Swift - An Introduction to Swift (20)

Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
Giuseppe Arici
 
Workshop Swift
Workshop Swift Workshop Swift
Workshop Swift
Commit University
 
Swift Programming
Swift ProgrammingSwift Programming
Swift Programming
Codemotion
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
Giuseppe Arici
 
Swift - the future of iOS app development
Swift - the future of iOS app developmentSwift - the future of iOS app development
Swift - the future of iOS app development
openak
 
Developing iOS apps with Swift
Developing iOS apps with SwiftDeveloping iOS apps with Swift
Developing iOS apps with Swift
New Generation Applications
 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
Jordan Morgan
 
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
Mark Simon
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift Overview
Kaz Yoshikawa
 
The swift programming language
The swift programming languageThe swift programming language
The swift programming language
Pardeep Chaudhary
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1
Hackraft
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
 
Introduction to Swift
Introduction to SwiftIntroduction to Swift
Introduction to Swift
Matteo Battaglio
 
Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swift
Pascal Batty
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
Jack Nutting
 
Deep Dive Into Swift
Deep Dive Into SwiftDeep Dive Into Swift
Deep Dive Into Swift
Sarath C
 
NUS iOS Swift Talk
NUS iOS Swift TalkNUS iOS Swift Talk
NUS iOS Swift Talk
Gabriel Lim
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Hossam Ghareeb
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.
Icalia Labs
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de Swift
Visual Engineering
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
Giuseppe Arici
 
Swift Programming
Swift ProgrammingSwift Programming
Swift Programming
Codemotion
 
Swift - the future of iOS app development
Swift - the future of iOS app developmentSwift - the future of iOS app development
Swift - the future of iOS app development
openak
 
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
Mark Simon
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift Overview
Kaz Yoshikawa
 
The swift programming language
The swift programming languageThe swift programming language
The swift programming language
Pardeep Chaudhary
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1
Hackraft
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
 
Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swift
Pascal Batty
 
Deep Dive Into Swift
Deep Dive Into SwiftDeep Dive Into Swift
Deep Dive Into Swift
Sarath C
 
NUS iOS Swift Talk
NUS iOS Swift TalkNUS iOS Swift Talk
NUS iOS Swift Talk
Gabriel Lim
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Hossam Ghareeb
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.
Icalia Labs
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de Swift
Visual Engineering
 
Ad

Recently uploaded (20)

Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptxFIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptxFIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Data Validation and System Interoperability
Data Validation and System InteroperabilityData Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptxFIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdfHigh Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdfENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptxFIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptxFIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Data Validation and System Interoperability
Data Validation and System InteroperabilityData Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptxFIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdfHigh Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdfENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Ad

NSCoder Swift - An Introduction to Swift

  • 1. NSCoder Swift An introduction to Swift Andreas Blick - @aquarioverde October 18th, 2014
  • 3. Swift • New programming language introduced by Apple on June 2014 at WWDC • Builds on the best of C and Objective-C, without the constraints of C compatibility • Development started by Chris Lattner, who also started the LLVM and Clang project
  • 4. Swift • Safe programming patterns and modern features • Seamless access to all existing Cocoa frameworks • Mix-and-match interoperability with Objective-C
  • 6. Playgrounds • Tool / Feature for • learning • code development • experimentation
  • 7. Playgrounds • Strings • Arrays & dictionaries • Color • Views • Images • … etc
  • 9. Variables & Constants • Defined using var or let keyword • No need to define type • Swift can mostly infer the type • Can contain any unicode character var instrument: String = “guitar” let nrOfSongs = 42 var trackLength = 3.23
  • 10. Strings • Collection of characters that can be accessed easily • Seamlessly bridged: NSString API available • Mutability defined by assignment (var or let) • String interpolation for literals, variables and expressions let instrument = “guitar" ! let music = "play (instrument)" let noise = "play " + instrument
  • 11. Numbers • Integers are stored as Int or UInt • can be written as decimal, binary, octal, hexadecimal • have max and min properties: Int.max • Floating point numbers are stored as Float and Double • can be written as decimal and hexadecimal • Can contain underscores for readability: 7_777.77 • Bool can be true or false
  • 12. Arrays • Stores ordered list of multiple values • Arrays are type specific • Accessed using properties, methods or subscripting var numbers: Array<Int> = Array<Int>() var numbers: [Int] = [Int]() ! numbers = [1,2,3] numbers = [Int](count:7,repeatedValue:0]
  • 13. Dictionaries • Containers that store multiple key-value pairs of the same type • Accessed using properties, methods or subscripting • Assigning nil as a value removes it var numbers:Dictionary<Int,String> = Dictionary<Int,String>() ! numbers = [1:“One”,2:“Two”,3:”Three”]
  • 14. Tuples • Group multiple values of any type into a single compound value • Values can be of any type • Values can be accessed by index • Tuples can have named values let instrument = ("guitar", “strings") println(instrument.0) ! let (name, family) = instrument ! let instrument = (name: "guitar", family: "strings")
  • 15. Loops • for-in loop to iterate using range • for-in loops to iterate over a collection • while / do-while loops if unknown number of iterations for ix in 1…5 { . . . } for var ix = 0; ix < 7; ix++ { . . . } for (key, value) in aDictionary { . . . } ! while ix < 7 { . . . } do { . . . } while ix < 7
  • 16. Conditionals • if / else for simple conditions • switch statements for complex conditions if number == 5 { . . . } ! switch number { case 5: println(“5”) default: println(“default”) }
  • 17. Conditionals - Switch • do not fall through (no break needed) • must be exhaustive (use default:) • support ranges: case 0…7: • support tuples: case (1, 2): • value binding: case (let x, 2): • can use where clause to check additional conditions case (let x, let y) where x == y:
  • 18. Control Transfer Statements • continue • break • return • fallthrough • loops and switches can be labeled to be used with break and continue counter: for i in 1...7 { for j in 1...7 { println("(i) - (j)") if i == 3 { continue counter} } }
  • 19. ?
  • 20. Optionals • Used where a value might be missing by adding a ? sign • Similar to nil in Objective-C, but can be used on any type • non optionals can not be nil • Optionals can implicitly be unwrapped by adding a ! sign (forced unwrapping) let instruments = ["guitar" : 6, "bass": 4, “ukulele”: “8”] let nrOfStrings: Int? = instruments[“guitar"] ! if nrOfStrings != nil { println(nrOfStrings!) } else { . . . }
  • 21. Optionals • Use optional binding to find out if an optional contains a value let instruments = ["guitar" : 6, "bass": 4, “ukulele”: “8”] let nrOfStrings: Int? = instruments[“guitar"] ! if let nrOfStrings = instruments["guitar"] { println(nrOfStrings) } else { . . . } • Use optional chaining when a value might be nil let familyName = guitar.family?.name?
  • 22. func
  • 23. Functions • Functions are blocks of code that execute a specific task • Functions have parameters and can return values - default to void • Parameters can have default values • They should always be placed at the end of the list func play(instrument: String = "guitar") -> String { return "play (instrument)" } ! play(instrument: "drums")
  • 24. Functions • Parameter names are not required when calling a function that has no default values • Functions can return multiple values using tuples • Use # if external and internal parameter names are the same func play(instrument: String) -> (String, volume: Int) { return ("play (instrument)", 7) } play(“guitar").volume ! func play(instrument instr: String) -> String { . . . } func play(#instrument: String) -> String { . . . }
  • 25. Functions • Functions can take a variable number of arguments using variadic parameters • One function can only have one variadic parameter func playInstruments(instruments: String...) -> String { var play = "playing " for instrument in instruments { play += instrument + " " } return play } playInstruments("guitar", "drums", "sax")
  • 26. Functions • Parameters are constants by default and can not be changed • They can be defined as variables using var • Variables parameters can only be modified within a function • To persist a variable outside a function the inout parameter is needed func swapTwoInts(inout a: Int, inout b: Int) { let tmpA = a a = b b = tmpA } var x = 3, y = 7 swapTwoInts(&x, &y)
  • 27. Functions • Functions have a specific function type that can be assigned to variables • They can be passed as parameters and returned by other functions • Functions can be nested func iPlay(player:(String) ->String, song:String) ->String { return player(song) } func playGuitar(song: String) -> String { return "playing (song) on guitar" } let playing = iPlay(playGuitar, "I sat by the ocean")
  • 29. Closures • Closures are self-contained blocks of code that can be passed around • Functions are named closures! • Closures can capture and store references and constants from their surrounding context • Trailing closures can be used for readability • Closures start using the in keyword
  • 30. Closures - Example let playMusic = { println("make some noise”) } ! ! ! func repeat(count:Int, song:String, player:(String)->(String)) { for i in 0..<count { println(player(song)); } } repeat(5, "Even Flow") { (song: String) -> String in return "playing (song)" }
  • 31. class
  • 32. Classes • Classes are program code templates for creating objects, consisting of properties and methods • No need for interface/header files • No need to inherit from any other class • No abstract class support • Compare using the identity operator ===
  • 33. Classes - Properties • Access to properties handled automatically • Properties do not have a corresponding instance variable • Every property needs to have a value assigned • Properties without a setter are read-only • Can have lazy stored properties (lazy) • Type properties are defined using class keyword
  • 34. Classes - Example class Instrument { let type: String var isUsed: Bool = false init(type: String) { self.type = type } var info: String { get { return isUsed ? "used (type)" : "new (type)" } } } ! let electricGuitar = Instrument(type: "guitar")
  • 35. Classes - Properties • Property observers can respond to changes in a properties’ value (willSet & didSet) var isUsed: Bool = false { willSet(willBeUsed) { let msg = (self.isUsed ? "was" : "was not") + " used and “ + (willBeUsed ? "is used now" : "is not used now”) println(msg) } }
  • 36. Classes - Methods • Methods are functions associated with a particular type • Subscripts are special methods (subscript) for accessing members. They can contain any number of input parameters of any type. class Instrument { func play() { println("make some noise") } subscript(ix: Int) -> String { return "model (ix)" } }
  • 37. Initialization • The class needs to be completely initialized before accessing any property or method • Properties that are allowed to have no value can be declared optional using the ? sign • Property observers are NOT called on initialization • Every class must have at least one designated initialiser • Every class can have multiple convenience initializers • Deinitializers can be used for cleaning up
  • 38. Initializer Chaining • Designated initializers must call a designated initializer from their immediate superclass • Convenience initializers must call another initializer available in the same class • Convenience initializers must ultimately end up calling a designated initializer
  • 39. Initialization - Example class Instrument { let type: String var family: String? init(type: String) { self.type = type } convenience init(type: String, family: String) { self.init(type: type) self.family = family } deinit { println("instrument (type) destroyed") } } ! let guitar = Instrument(type: “guitar")
  • 40. Initialization • Closures can be used for initialising complex properties class Some { let some: String = { return "some" }() }
  • 41. Inheritance • Classes can inherit methods, properties from other classes • Initializers are not inherited by default • Initialize own class properties before calling super.init • Overridden methods need override keyword • Methods marked as final can not be overridden • An entire class can be marked as final
  • 42. Inheritance - Example class Guitar : Instrument { init() { super.init(type: "guitar") family = "Strings" } ! override func play() { println("make some noise") } } ! let guitar = Guitar()
  • 44. Structures • Structures are value types, they are always copied, not referenced • Structures can have initializers, methods and properties • Automatically generated memberwise initializer • Structures do not have deinitializers • Array and Dictionary are structures
  • 45. Structures • All properties of constant structures are constant as well • Methods for modifying structure properties need the mutating keyword • Structures do not support inheritance • No type-casting • Type properties are defined using static keyword
  • 46. Enumerations • An enumeration is a complete collection of items • Enumeration values are fully-fledged in their own • Enumerations are first-class types! • Enumerations can have methods & initialisers • Enumeration can have default raw values of the same type
  • 47. Enumerations - Example enum Instrument: Int { case Guitar = 1, Base case Drums, Bongo, Trumpet, Saxophone func description() -> String { switch self { case .Guitar, .Base: return "Strings" case .Trumpet, .Saxophone: return "Wind" default: return "Unknown" } } } let instrument = Instrument.Trumpet instrument.description() instrument.toRaw() let unknown = Instrument.fromRaw(1)?.description()
  • 48. Enumerations • Enumerations can have associated values enum Instrument { case Guitar(nrOfStrings: Int), Base case Drums, Bongo func description() -> String { switch self { case .Guitar(let nrOfStrings): return ("(nrOfStrings)-string guitar") default: return "Unknown" } } } let instrument = Instrument.Guitar(nrOfStrings: 6) instrument.description()
  • 49. ++
  • 50. Extensions • Extensions add new functionality to existing classes, structures, … etc. They are similar to Categories in Objective-C • They can add methods, computed properties, initialisers but no stored properties extension Instrument { func rock() { println("(type) is rocking") } }
  • 51. Protocols • A protocol defines methods and properties a class, struct or enumeration can or must adopt • Existing types can be extended to adopt a protocol • A protocol can inherit from one or more other protocols • Protocol composition can be used to combine protocols • Any protocol can be used as type • Define as @objc for type checking and optional support
  • 52. Protocols - Example @objc protocol Instrument { func play() } ! class Guitar: Instrument { func play() { println("playing guitar") } } ! let items = [Guitar(), NSString()] ! for item in items { if let instrument = item as? Instrument { instrument.play() } }
  • 53. Generics • Generic code enables writing flexible, reusable functions and types func swapTwoValues<T>(inout a: T, inout b: T) { let tmpA = a a = b b = a }
  • 54. Generics • You can define your own generic type • and specify type constraints struct Stack<T: Instrument> { var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() { items.removeLast() } } var guitarStack = Stack<Guitar>()
  • 55. References • WWDC 2014 Videos • Apple Books • The Swift Programming Language • Using Swift with Cocoa & Objective-C • Web • https://developer.apple.com/swift/
  • 56. +++
  • 57. ARC • Swift handles memory management using automatic reference counting • Think about relationship between object instead of memory • Avoid strong reference cycles by using weak or unowned references
  • 58. Assertions • Use assertions whenever a condition has the potential to be false, but must be true • End code execution let age = -3 assert(age >= 0, "age can no be less than 0")
  • 59. Thank you! Andreas Blick - @aquarioverde