SlideShare a Scribd company logo
Welcome to Swift
Carl Brown (@CarlBrwn)
CocoaCoder.org, 12 June 2014 (WWDC Keynote + 10 days)
Disclaimer
• We’ve known of this
language’s existence for less
than two weeks
• I’ve yet to ship a project with it
• We’ve been told the language
will be changing over time
*image: http://vectorgoods.com/wp-content/uploads/2012/01/nuclear-danger-vector.jpg
Event Interactivity
Please(!) stop me if you have
questions
http://farm3.static.flickr.com/2197/2200500024_e93db99b61.jpg
What is Swift?
New Programming Language from Apple
Announced at WWDC 2014
After being worked on in secret for 4 years
Interoperates with (and may eventually replace) Objective-C
Currently still a work in progress
Looks a lot like a scripting language*
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
}
}
println ("the score is (teamScore)")
*but it’s compiled
Swift != Obj-C
Goodbye Square Brackets*
!
…and semicolons
*Except Array definitions and subscripts
Standard-ish Types
var int1: Int = 30
var float1: Float = 30.5
var bool1: Bool = false
var str1: String = "Hello, playground"
var array1: Array = ["Hi", 40]
var dict1: Dictionary <String,String> = [ "key" : "value"]
Type-safe (and types can be implied)
Swift Still has Immutability
let individualScore = 75
!
!
var teamScore = 0
Read-Only
Read-Write
*Array immutability seems complicated for performance reasons
Loops
var firstForLoop = 0
for i in 0..3 {
firstForLoop += i
}
var n = 2
while n < 100 {
n = n * 2
}
var m = 2
do {
m = m * 2
} while m < 100
let individualScores = [75, 43, 12]
for score in individualScores {
teamScore += 3
}
Switch directly on Objects
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins."
case "cucumber", "watercress":
let vegetableComment = "Make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy (x)?"
default:
let vegetableComment = "Everything okay in soup."
}
*All possibilities MUST be covered or compiler error
“Tuples”
let (statusCode, statusMessage) = http404Error
let http404Error = (404, "Not Found")
println("The status code is (statusCode)")
// prints "The status code is 404"
println("The status message is (statusMessage)")
// prints "The status message is Not Found”
*This Excerpt (and many others) From: Apple Inc. “The Swift Programming Language.” iBooks.
Compiler Enforces Initialization
“Optionals”
Primitive Types can’t be nil
Types have “Optional” versions (nil or value)
You have to “Unwrap” Optionals before you can use them in non-optional
context
Convention:
Use “if let” to “Unwrap”
var optionalString: String?
!
if let definiteString = optionalString {
println(definiteString)
}
*You can also use (!) and risk a run-time crash
“Generics”–Strongly Typed Collections
Generic Dictionaries, too
“Closures”
var strings = ["a","b","c","d"]
let uppercaseStrings = strings.map {
(s1: String) -> String in
return s1.uppercaseString
}
//["A", "B", "C", "D"]
Functions
func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
getGasPrices()
*This Excerpt (and many others) From: Apple Inc. “The Swift Programming Language.” iBooks.
Classes/Objects
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with (numberOfSides) sides."
}
}
var shape = NamedShape(name: "square")
shape.numberOfSides = 4
Interoperability
let path="/tmp/stringFile.txt"
!
let content : AnyObject? = NSString.stringWithContentsOfFile(path)
!
if let contentString = content as? String {
contentString
}

More Related Content

What's hot (6)

PPTX
10 tips for making Bash a sane programming language
Yaroslav Tkachenko
 
PDF
Akka persistence == event sourcing in 30 minutes
Konrad Malawski
 
PDF
ZendCon 2017 - Build a Bot Workshop - Async Primer
Adam Englander
 
PPT
appache_1
Maisa Al-Khudair
 
PDF
Yaroslav Martsynyuk - Deploying Elixir/Phoenix with Distillery
Elixir Club
 
PDF
Akka Futures and Akka Remoting
Knoldus Inc.
 
10 tips for making Bash a sane programming language
Yaroslav Tkachenko
 
Akka persistence == event sourcing in 30 minutes
Konrad Malawski
 
ZendCon 2017 - Build a Bot Workshop - Async Primer
Adam Englander
 
appache_1
Maisa Al-Khudair
 
Yaroslav Martsynyuk - Deploying Elixir/Phoenix with Distillery
Elixir Club
 
Akka Futures and Akka Remoting
Knoldus Inc.
 

Viewers also liked (20)

PPTX
A Few Interesting Things in Apple's Swift Programming Language
SmartLogic
 
PDF
How to make workout app for watch os 2
Yusuke Kita
 
PPTX
Adapt or Die DevJam: San Francisco, Sept 27 2016
Apigee | Google Cloud
 
PPTX
Mindbody: A Digital Transformation Story
Apigee | Google Cloud
 
PPTX
London Adapt or Die: Opening Keynote with Chet Kapoor
Apigee | Google Cloud
 
PDF
From IoT to biohacking. Trends that were, are and will be popular.
Natalia Hatalska
 
PPTX
London adapt or-die opening keynote chet kapoor
Apigee | Google Cloud
 
PDF
London Adapt or Die: Lunch keynote
Apigee | Google Cloud
 
PDF
Linux PV on HVM
The Linux Foundation
 
PPTX
Becoming the Uncarrier: T-Mobile's Digital Journey
Apigee | Google Cloud
 
PPTX
API Governance in the Enterprise
Apigee | Google Cloud
 
PPTX
London Adapt or Die: Five Things Enterprises Should Know About Serverless
Apigee | Google Cloud
 
PPTX
API Management and Kubernetes
Apigee | Google Cloud
 
PPTX
London Adapt or Die: Securing your APIs the Right Way!
Apigee | Google Cloud
 
PDF
Adapt or Die: Keynote with Anant Jhingran
Apigee | Google Cloud
 
PPTX
Managing Sensitive Information in an API and Microservices World
Apigee | Google Cloud
 
PDF
Adapt or Die: Keynote with Greg Brail
Apigee | Google Cloud
 
PPTX
Adapt or Die Sydney - 5 Things Developers Should Know About Serverless
Apigee | Google Cloud
 
PDF
London Adapt or Die: Closing Keynote — Adapt Now!
Apigee | Google Cloud
 
PDF
Adapt or Die: Opening Keynote with Chet Kapoor
Apigee | Google Cloud
 
A Few Interesting Things in Apple's Swift Programming Language
SmartLogic
 
How to make workout app for watch os 2
Yusuke Kita
 
Adapt or Die DevJam: San Francisco, Sept 27 2016
Apigee | Google Cloud
 
Mindbody: A Digital Transformation Story
Apigee | Google Cloud
 
London Adapt or Die: Opening Keynote with Chet Kapoor
Apigee | Google Cloud
 
From IoT to biohacking. Trends that were, are and will be popular.
Natalia Hatalska
 
London adapt or-die opening keynote chet kapoor
Apigee | Google Cloud
 
London Adapt or Die: Lunch keynote
Apigee | Google Cloud
 
Linux PV on HVM
The Linux Foundation
 
Becoming the Uncarrier: T-Mobile's Digital Journey
Apigee | Google Cloud
 
API Governance in the Enterprise
Apigee | Google Cloud
 
London Adapt or Die: Five Things Enterprises Should Know About Serverless
Apigee | Google Cloud
 
API Management and Kubernetes
Apigee | Google Cloud
 
London Adapt or Die: Securing your APIs the Right Way!
Apigee | Google Cloud
 
Adapt or Die: Keynote with Anant Jhingran
Apigee | Google Cloud
 
Managing Sensitive Information in an API and Microservices World
Apigee | Google Cloud
 
Adapt or Die: Keynote with Greg Brail
Apigee | Google Cloud
 
Adapt or Die Sydney - 5 Things Developers Should Know About Serverless
Apigee | Google Cloud
 
London Adapt or Die: Closing Keynote — Adapt Now!
Apigee | Google Cloud
 
Adapt or Die: Opening Keynote with Chet Kapoor
Apigee | Google Cloud
 
Ad

Similar to Welcome to Swift (CocoaCoder 6/12/14) (20)

PDF
Cool Jvm Tools to Help you Test - Aylesbury Testers Version
Schalk Cronjé
 
PDF
Cool JVM Tools to Help You Test
Schalk Cronjé
 
PDF
HTML5 for the Silverlight Guy
David Padbury
 
PDF
Gojko Adzic Cucumber
Skills Matter
 
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
KEY
Node.js
Mat Schaffer
 
PPTX
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
PPTX
Enterprise JavaScript ... what the heck?
Nedelcho Delchev
 
PPTX
All a flutter about Flutter.io
Steven Cooper
 
PDF
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
jaxLondonConference
 
PDF
Anatomy of a Gem: Bane
Daniel Wellman
 
PPTX
Coffee scriptisforclosers nonotes
Brandon Satrom
 
PDF
Cucumber & BDD
Sam Davarnia
 
PDF
Cucumber: 小黃瓜驗收測試工具
Wen-Tien Chang
 
PDF
Trying Continuous Delivery - pyconjp 2012
Toru Furukawa
 
PDF
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Codemotion
 
PPTX
Writing Hadoop Jobs in Scala using Scalding
Toni Cebrián
 
PDF
Be More Productive with Kotlin
Brandon Wever
 
PPT
Goodparts
damonjablons
 
Cool Jvm Tools to Help you Test - Aylesbury Testers Version
Schalk Cronjé
 
Cool JVM Tools to Help You Test
Schalk Cronjé
 
HTML5 for the Silverlight Guy
David Padbury
 
Gojko Adzic Cucumber
Skills Matter
 
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
Node.js
Mat Schaffer
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
Enterprise JavaScript ... what the heck?
Nedelcho Delchev
 
All a flutter about Flutter.io
Steven Cooper
 
Real-world polyglot programming on the JVM - Ben Summers (ONEIS)
jaxLondonConference
 
Anatomy of a Gem: Bane
Daniel Wellman
 
Coffee scriptisforclosers nonotes
Brandon Satrom
 
Cucumber & BDD
Sam Davarnia
 
Cucumber: 小黃瓜驗收測試工具
Wen-Tien Chang
 
Trying Continuous Delivery - pyconjp 2012
Toru Furukawa
 
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Codemotion
 
Writing Hadoop Jobs in Scala using Scalding
Toni Cebrián
 
Be More Productive with Kotlin
Brandon Wever
 
Goodparts
damonjablons
 
Ad

More from Carl Brown (20)

PDF
GDPR, User Data, Privacy, and Your Apps
Carl Brown
 
PDF
New in iOS 11.3b4 and Xcode 9.3b4
Carl Brown
 
PDF
Managing Memory in Swift (Yes, that's a thing)
Carl Brown
 
PDF
Better Swift from the Foundation up #tryswiftnyc17 09-06
Carl Brown
 
PDF
Generics, the Swift ABI and you
Carl Brown
 
PDF
Swift GUI Development without Xcode
Carl Brown
 
PDF
what's new in iOS10 2016-06-23
Carl Brown
 
PDF
Open Source Swift: Up and Running
Carl Brown
 
PDF
Parse migration CocoaCoders April 28th, 2016
Carl Brown
 
PDF
Swift 2.2 Design Patterns CocoaConf Austin 2016
Carl Brown
 
PDF
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Carl Brown
 
PDF
Gcd cc-150205
Carl Brown
 
PDF
Cocoa coders 141113-watch
Carl Brown
 
PDF
iOS8 and the new App Store
Carl Brown
 
PDF
Dark Art of Software Estimation 360iDev2014
Carl Brown
 
PDF
Intro to cloud kit Cocoader.org 24 July 2014
Carl Brown
 
PDF
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Carl Brown
 
PPT
Introduction to Git Commands and Concepts
Carl Brown
 
PDF
REST/JSON/CoreData Example Code - A Tour
Carl Brown
 
KEY
360iDev iOS AntiPatterns
Carl Brown
 
GDPR, User Data, Privacy, and Your Apps
Carl Brown
 
New in iOS 11.3b4 and Xcode 9.3b4
Carl Brown
 
Managing Memory in Swift (Yes, that's a thing)
Carl Brown
 
Better Swift from the Foundation up #tryswiftnyc17 09-06
Carl Brown
 
Generics, the Swift ABI and you
Carl Brown
 
Swift GUI Development without Xcode
Carl Brown
 
what's new in iOS10 2016-06-23
Carl Brown
 
Open Source Swift: Up and Running
Carl Brown
 
Parse migration CocoaCoders April 28th, 2016
Carl Brown
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Carl Brown
 
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Carl Brown
 
Gcd cc-150205
Carl Brown
 
Cocoa coders 141113-watch
Carl Brown
 
iOS8 and the new App Store
Carl Brown
 
Dark Art of Software Estimation 360iDev2014
Carl Brown
 
Intro to cloud kit Cocoader.org 24 July 2014
Carl Brown
 
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Carl Brown
 
Introduction to Git Commands and Concepts
Carl Brown
 
REST/JSON/CoreData Example Code - A Tour
Carl Brown
 
360iDev iOS AntiPatterns
Carl Brown
 

Welcome to Swift (CocoaCoder 6/12/14)

  • 1. Welcome to Swift Carl Brown (@CarlBrwn) CocoaCoder.org, 12 June 2014 (WWDC Keynote + 10 days)
  • 2. Disclaimer • We’ve known of this language’s existence for less than two weeks • I’ve yet to ship a project with it • We’ve been told the language will be changing over time *image: http://vectorgoods.com/wp-content/uploads/2012/01/nuclear-danger-vector.jpg
  • 3. Event Interactivity Please(!) stop me if you have questions http://farm3.static.flickr.com/2197/2200500024_e93db99b61.jpg
  • 4. What is Swift? New Programming Language from Apple Announced at WWDC 2014 After being worked on in secret for 4 years Interoperates with (and may eventually replace) Objective-C Currently still a work in progress
  • 5. Looks a lot like a scripting language* let individualScores = [75, 43, 103, 87, 12] var teamScore = 0 for score in individualScores { if score > 50 { teamScore += 3 } else { teamScore += 1 } } println ("the score is (teamScore)") *but it’s compiled
  • 6. Swift != Obj-C Goodbye Square Brackets* ! …and semicolons *Except Array definitions and subscripts
  • 7. Standard-ish Types var int1: Int = 30 var float1: Float = 30.5 var bool1: Bool = false var str1: String = "Hello, playground" var array1: Array = ["Hi", 40] var dict1: Dictionary <String,String> = [ "key" : "value"]
  • 8. Type-safe (and types can be implied)
  • 9. Swift Still has Immutability let individualScore = 75 ! ! var teamScore = 0 Read-Only Read-Write *Array immutability seems complicated for performance reasons
  • 10. Loops var firstForLoop = 0 for i in 0..3 { firstForLoop += i } var n = 2 while n < 100 { n = n * 2 } var m = 2 do { m = m * 2 } while m < 100 let individualScores = [75, 43, 12] for score in individualScores { teamScore += 3 }
  • 11. Switch directly on Objects let vegetable = "red pepper" switch vegetable { case "celery": let vegetableComment = "Add some raisins." case "cucumber", "watercress": let vegetableComment = "Make a good tea sandwich." case let x where x.hasSuffix("pepper"): let vegetableComment = "Is it a spicy (x)?" default: let vegetableComment = "Everything okay in soup." } *All possibilities MUST be covered or compiler error
  • 12. “Tuples” let (statusCode, statusMessage) = http404Error let http404Error = (404, "Not Found") println("The status code is (statusCode)") // prints "The status code is 404" println("The status message is (statusMessage)") // prints "The status message is Not Found” *This Excerpt (and many others) From: Apple Inc. “The Swift Programming Language.” iBooks.
  • 14. “Optionals” Primitive Types can’t be nil Types have “Optional” versions (nil or value) You have to “Unwrap” Optionals before you can use them in non-optional context
  • 15. Convention: Use “if let” to “Unwrap” var optionalString: String? ! if let definiteString = optionalString { println(definiteString) } *You can also use (!) and risk a run-time crash
  • 18. “Closures” var strings = ["a","b","c","d"] let uppercaseStrings = strings.map { (s1: String) -> String in return s1.uppercaseString } //["A", "B", "C", "D"]
  • 19. Functions func getGasPrices() -> (Double, Double, Double) { return (3.59, 3.69, 3.79) } getGasPrices() *This Excerpt (and many others) From: Apple Inc. “The Swift Programming Language.” iBooks.
  • 20. Classes/Objects class NamedShape { var numberOfSides: Int = 0 var name: String init(name: String) { self.name = name } func simpleDescription() -> String { return "A shape with (numberOfSides) sides." } } var shape = NamedShape(name: "square") shape.numberOfSides = 4
  • 21. Interoperability let path="/tmp/stringFile.txt" ! let content : AnyObject? = NSString.stringWithContentsOfFile(path) ! if let contentString = content as? String { contentString }