SlideShare a Scribd company logo
Introduction to Swift
Lecture 02
Jonathan R. Engelsma, Ph.D.
TOPICS
• Swift Language Overview
• Our first program!
• Variables and constants
• Strings
• CollectionTypes
• Control Flow
• Functions
THE SWIFT PROGRAMMING
LANGUAGE
• A surprise introduced by Apple @ WWDC in June 2014.
• An industrial-quality systems programming language that is as
expressive and enjoyable to use as a scripting language.
• Seamless access to existing Cocoa frameworks.
• Successor to legacy Objective-C language, but provides mix-
and-match interoperability with Objective-C code.
WHY A NEW LANGUAGE?
• Easier for new programmers to get up to speed in iOS
development.
• New language was necessary in order to ensure compatibility
with existing frameworks.
• Easier to create a new language that feels modern, but still
adapts to Objective-C / C conventions.
SWIFT CHARACTERISTICS
• Compiled: source —> bytecodes, not interpreted.
• Strong and static typing: types are clearly identified and
cannot change (e.g. compiler generates faster safer code).
• Automatic ref counting: objects are automatically freed from
memory when there are no more references.
• Name-spaced: makes our code easier to coexist with other
people’s code.
OUR OBLIGATORY FIRST
PROGRAM!
• a complete Swift program!
• no imports / includes!
• no main program - code
written at global scope
becomes the entry point.
• No semicolons!
CONSTANTS
• Use the “let” statement to define constants.
• values doesn’t need to be known at compile time, but can be
assigned only once.
• value assigned must be the same type as the variable name.
• Types can be implicit or explicit.
VARIABLES
• Use the “var” statement to define variables - values can be
mutated!
• value assigned must be the same type as the variable name.
• Types can be implicit or explicit.
• Type conversion is explicit.
STRINGS
• String type is an ordered collection of Character:“hello, world”
• Bridged to NSString in Objective-C.
• String is a value type: copied when passed to function or
method. (different than NSString in this regard!)
• Strings defined with var can be mutated, strings defined with let
cannot be mutated!
STRINGS
• concatenation: can be accomplished with the + operator
• interpolation: can construct new strings from a mix of values/
expressions. using ().
• Compare using == operator.
• Use the hasPrefix / hasSuffix methods to check how a string
starts/ends.
STRINGS
ARRAYS IN SWIFT
• Arrays store ordered lists of values
• All values in a given array are of the same type
• Values are accessed via methods, properties and subscripting.
ARRAYS
DICTIONARIES IN SWIFT
• Dictionaries store multiple values of the same type
• Each value is associated with a unique key
• Do not have to be in a specified order
• Use just like a real world dictionary: e.g. when you want to
look up the definition of a particular key value
• Keys must be hashable
DICTIONARIES
CONTROL FLOW
• for-in statements:
CONTROL FLOW
• while & do-while statements:
CONTROL FLOW
• if / if-else
SWITCH STATEMENT
• Similar to C syntax, but with some notable differences:
• No need to use break statements!
• No implicit fall through
• Each case must contain at least one executable statement
• Case condition can be scalar, range, or tuple!
• Case condition can actually bind values.
CONTROL FLOW
• switch statement
FUNCTIONS
• Self-contained chunks of code that perform a specific task.
• Functions are typed by the return type and type of the
parameters.
• Swift supports first-class functions, e.g. functions can be passed
as arguments and serve as return values.
• Functions in Swift can be nested.
• Swift functions can have multiple return values.
FUNCTIONS
FUNCTIONS
FUNCTIONS
Returning multiple values from a function:
FUNCTIONS
• So far, all the functions we’ve seen defined what are known as
local parameter names. E.g. they are only referred to in the
function implementation.
• Sometimes its useful to name parameters when you call a
function. These are called external parameter names.
FUNCTIONS
• external parameter names:
FUNCTIONS
• shorthand for external parameter names:
FUNCTIONTYPES
• Variable that refer to functions can be defined:
FUNCTIONTYPES
• Functions can be passed as parameters:
FUNCTIONTYPES
• Functions can be returned from functions:
NESTED FUNCTIONS
• We can rewrite the previous examples with nested funcs:

More Related Content

PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
PDF
Swift, a quick overview
PPTX
Intro to Scala
PPTX
Beginning Java for .NET developers
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
PPTX
Scala-Ls1
PDF
2 BytesC++ course_2014_c6_ constructors and other tools
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
Swift, a quick overview
Intro to Scala
Beginning Java for .NET developers
Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#
Scala-Ls1
2 BytesC++ course_2014_c6_ constructors and other tools

What's hot (18)

PPTX
Scala’s implicits
PPTX
AngularConf2015
PDF
Preparing for Scala 3
PDF
Variables in Pharo5
PDF
Diving Into Scala Cats - Semigroups and Monoids
PPTX
Introduction of lambda expression and predicate builder
PPTX
Java tokens
PDF
Python's dynamic nature (rough slides, November 2004)
PPTX
Swift
PPTX
iOS development using Swift - Swift Basics (1)
PPT
Learning typescript
PDF
What To Leave Implicit
PPTX
iOS development using Swift - Swift Basics (2)
PPT
Clojure's take on concurrency
PPTX
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
PPTX
Typescript in 30mins
PDF
New c sharp4_features_part_v
Scala’s implicits
AngularConf2015
Preparing for Scala 3
Variables in Pharo5
Diving Into Scala Cats - Semigroups and Monoids
Introduction of lambda expression and predicate builder
Java tokens
Python's dynamic nature (rough slides, November 2004)
Swift
iOS development using Swift - Swift Basics (1)
Learning typescript
What To Leave Implicit
iOS development using Swift - Swift Basics (2)
Clojure's take on concurrency
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
Typescript in 30mins
New c sharp4_features_part_v
Ad

Viewers also liked (15)

PDF
Swift Programming Language
PDF
Swift language - A fast overview of some features
PPTX
IOS Swift language 2nd tutorial
PDF
What Every IT Manager Should Know About Mobile Apps
PDF
2013 Michigan Beekeepers Association Annual Spring Conference
PDF
Mobile Gamification
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 04)
PDF
Knowing Your Bees: Becoming a Better Beekeeper
PDF
2012 Michigan Beekeepers Association Annual Spring Conference - Beekeepers On...
PPTX
Beginning iOS Development with Swift
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 7)
PDF
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
PDF
Workhop iOS 1: Fundamentos de Swift
PDF
Никита Корчагин - iOS development information
Swift Programming Language
Swift language - A fast overview of some features
IOS Swift language 2nd tutorial
What Every IT Manager Should Know About Mobile Apps
2013 Michigan Beekeepers Association Annual Spring Conference
Mobile Gamification
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 04)
Knowing Your Bees: Becoming a Better Beekeeper
2012 Michigan Beekeepers Association Annual Spring Conference - Beekeepers On...
Beginning iOS Development with Swift
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 7)
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Workhop iOS 1: Fundamentos de Swift
Никита Корчагин - iOS development information
Ad

Similar to iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 02) (20)

PDF
Swift, swiftly
PDF
Intro toswift1
PDF
Introducing Swift v2.1
PDF
SV-ios-objc-to-swift
PPTX
presentationofswift.pptx
PDF
Swift Programming
PDF
Swift Introduction
PDF
Swift rocks! #1
PDF
Introduction to Swift
PDF
Swift - the future of iOS app development
PDF
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
PDF
Workshop Swift
PDF
The swift programming language
PPT
Swift Basics with iOS 8 features
PDF
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
PDF
Quick swift tour
PDF
Objective-C to Swift - Swift Cloud Workshop 3
PDF
Introduction to Swift 2
PPTX
Introduction to Swift (tutorial)
Swift, swiftly
Intro toswift1
Introducing Swift v2.1
SV-ios-objc-to-swift
presentationofswift.pptx
Swift Programming
Swift Introduction
Swift rocks! #1
Introduction to Swift
Swift - the future of iOS app development
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Workshop Swift
The swift programming language
Swift Basics with iOS 8 features
The Swift Programming Language - Xcode6 for iOS App Development - AgileInfowa...
Quick swift tour
Objective-C to Swift - Swift Cloud Workshop 3
Introduction to Swift 2
Introduction to Swift (tutorial)

More from Jonathan Engelsma (7)

PDF
BIP Hive Scale Program Overview
PDF
Selling Honey Online
PDF
Selling Honey at Farmers Markets, Expos, etc.
PDF
Harvesting and Handling Honey for Hobby and Small Sideline Beekeepers
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 09)
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 06)
PDF
So You Want To Be a Beekeeper?
BIP Hive Scale Program Overview
Selling Honey Online
Selling Honey at Farmers Markets, Expos, etc.
Harvesting and Handling Honey for Hobby and Small Sideline Beekeepers
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 09)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 06)
So You Want To Be a Beekeeper?

Recently uploaded (20)

PDF
Digital Strategies for Manufacturing Companies
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
System and Network Administration Chapter 2
PDF
top salesforce developer skills in 2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
System and Network Administraation Chapter 3
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
history of c programming in notes for students .pptx
PPTX
Transform Your Business with a Software ERP System
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Reimagine Home Health with the Power of Agentic AI​
Digital Strategies for Manufacturing Companies
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Migrate SBCGlobal Email to Yahoo Easily
wealthsignaloriginal-com-DS-text-... (1).pdf
System and Network Administration Chapter 2
top salesforce developer skills in 2025.pdf
Operating system designcfffgfgggggggvggggggggg
How Creative Agencies Leverage Project Management Software.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
System and Network Administraation Chapter 3
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
history of c programming in notes for students .pptx
Transform Your Business with a Software ERP System
Design an Analysis of Algorithms II-SECS-1021-03
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Reimagine Home Health with the Power of Agentic AI​

iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 02)

  • 1. Introduction to Swift Lecture 02 Jonathan R. Engelsma, Ph.D.
  • 2. TOPICS • Swift Language Overview • Our first program! • Variables and constants • Strings • CollectionTypes • Control Flow • Functions
  • 3. THE SWIFT PROGRAMMING LANGUAGE • A surprise introduced by Apple @ WWDC in June 2014. • An industrial-quality systems programming language that is as expressive and enjoyable to use as a scripting language. • Seamless access to existing Cocoa frameworks. • Successor to legacy Objective-C language, but provides mix- and-match interoperability with Objective-C code.
  • 4. WHY A NEW LANGUAGE? • Easier for new programmers to get up to speed in iOS development. • New language was necessary in order to ensure compatibility with existing frameworks. • Easier to create a new language that feels modern, but still adapts to Objective-C / C conventions.
  • 5. SWIFT CHARACTERISTICS • Compiled: source —> bytecodes, not interpreted. • Strong and static typing: types are clearly identified and cannot change (e.g. compiler generates faster safer code). • Automatic ref counting: objects are automatically freed from memory when there are no more references. • Name-spaced: makes our code easier to coexist with other people’s code.
  • 6. OUR OBLIGATORY FIRST PROGRAM! • a complete Swift program! • no imports / includes! • no main program - code written at global scope becomes the entry point. • No semicolons!
  • 7. CONSTANTS • Use the “let” statement to define constants. • values doesn’t need to be known at compile time, but can be assigned only once. • value assigned must be the same type as the variable name. • Types can be implicit or explicit.
  • 8. VARIABLES • Use the “var” statement to define variables - values can be mutated! • value assigned must be the same type as the variable name. • Types can be implicit or explicit. • Type conversion is explicit.
  • 9. STRINGS • String type is an ordered collection of Character:“hello, world” • Bridged to NSString in Objective-C. • String is a value type: copied when passed to function or method. (different than NSString in this regard!) • Strings defined with var can be mutated, strings defined with let cannot be mutated!
  • 10. STRINGS • concatenation: can be accomplished with the + operator • interpolation: can construct new strings from a mix of values/ expressions. using (). • Compare using == operator. • Use the hasPrefix / hasSuffix methods to check how a string starts/ends.
  • 12. ARRAYS IN SWIFT • Arrays store ordered lists of values • All values in a given array are of the same type • Values are accessed via methods, properties and subscripting.
  • 14. DICTIONARIES IN SWIFT • Dictionaries store multiple values of the same type • Each value is associated with a unique key • Do not have to be in a specified order • Use just like a real world dictionary: e.g. when you want to look up the definition of a particular key value • Keys must be hashable
  • 16. CONTROL FLOW • for-in statements:
  • 17. CONTROL FLOW • while & do-while statements:
  • 18. CONTROL FLOW • if / if-else
  • 19. SWITCH STATEMENT • Similar to C syntax, but with some notable differences: • No need to use break statements! • No implicit fall through • Each case must contain at least one executable statement • Case condition can be scalar, range, or tuple! • Case condition can actually bind values.
  • 21. FUNCTIONS • Self-contained chunks of code that perform a specific task. • Functions are typed by the return type and type of the parameters. • Swift supports first-class functions, e.g. functions can be passed as arguments and serve as return values. • Functions in Swift can be nested. • Swift functions can have multiple return values.
  • 25. FUNCTIONS • So far, all the functions we’ve seen defined what are known as local parameter names. E.g. they are only referred to in the function implementation. • Sometimes its useful to name parameters when you call a function. These are called external parameter names.
  • 27. FUNCTIONS • shorthand for external parameter names:
  • 28. FUNCTIONTYPES • Variable that refer to functions can be defined:
  • 29. FUNCTIONTYPES • Functions can be passed as parameters:
  • 30. FUNCTIONTYPES • Functions can be returned from functions:
  • 31. NESTED FUNCTIONS • We can rewrite the previous examples with nested funcs: