SlideShare a Scribd company logo
Swift 
Apple’s New Programming Language 
Sasha Goldshtein 
CTO, Sela Group 
blog.sashag.net @goldshtn 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Apple WWDC Announcements 
• iOS 8 and OS X Yosemite 
• Xcode 6 
• Swift 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Minus Bazillion Points 
“Every feature starts out in the hole by 100 
points, which means that it has to have a 
significant net positive effect on the overall 
package for it to make it into the language.” 
– Eric Gunnerson, C# compiler team 
If a language feature starts at -100 points, 
where does a new language start?! 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Objective-C Popularity 
• Objective-C owes all its popularity to the 
meteoric rise of the iPhone 
App Store introduced 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
What’s Wrong with Objective-C 
Exhibit One 
// Ignore the horrible syntax for a second. 
// But here's the charmer: 
// 1) if 'str' is null, we return NSNotFound (232-1) 
// 2) if 'arr' is null, we return 0 
+ (NSUInteger)indexOfString:(NSString *)str 
inArray:(NSArray *)arr 
{ 
return [arr indexOfObject:str]; 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
What’s Wrong with Objective-C 
Exhibit Two 
// This compiles and crashes at runtime with: 
// "-[__NSArrayI addObject:]: unrecognized selector 
// sent to instance 0x7a3141c0" 
NSMutableArray *array = [NSArray array]; 
[array addObject:@"hello"]; 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
What’s Wrong with Objective-C 
Exhibit Three 
NSMutableArray *array = [NSMutableArray array]; 
[array addObject:@"hello"]; 
[array addObject:@17]; 
// This compiles just fine 
NSString *string = array[1]; // Yes, the "17" 
// This works and prints 17 
NSLog(@"%@", string); 
// But this crashes 
NSLog(@"%@", 
[string stringByAppendingString:@"hmm"]); 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Swift Language Principles 
• Clean and modern 
• Type-safe and generic 
• Extensible 
• Functional 
• Productive 
• Compiled to native 
• Seamlessly interops with Objective-C 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Why Not an Existing Language? 
• Good philosophical question 
• If you want some more philosophy: 
http://s.sashag.net/why-swift 
We will now review some of the interesting 
language decisions, which make Swift 
different from mainstream languages 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Variables and Constants 
let size = 42 
let name = "Dave" 
var address = "14 Franklin Way" 
address = "16 Calgary Drive" 
var city: String 
city = "London" 
var pi: Double = 3.14 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Strings and Arrays 
var fullAddress = city + " " + address 
println("(name)'s at (city.capitalizedString)") 
var customers = ["Dave", "Kate", "Jack"] 
var addresses = [String]() 
addresses += fullAddress 
if customers[2].hasPrefix("Ja") { 
customers.removeAtIndex(2) 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Dictionaries 
var orderSummary = [ 
"Dave": 11000, 
"Kate": 14000, 
"Jack": 13500 
] 
orderSummary["Kate"] = 17500 
for (customer, orders) in orderSummary { 
println("(customer) -> (orders)") 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Null 
“I call it my billion-dollar mistake. It was the 
invention of the null reference in 1965. […] I 
couldn't resist the temptation to put in a null 
reference, simply because it was so easy to 
implement. This has led to innumerable 
errors, vulnerabilities, and system crashes, 
which have probably caused a billion dollars 
of pain and damage in the last forty years.” 
– Sir Charles Antony Richard Hoare, 
2009 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Optional Types 
var myName: String 
myName = nil // Does not compile! 
var jeffsOrders: Int? 
jeffsOrders = orderSummary["Jeff”] 
if jeffsOrders { 
let orders: Int = jeffsOrders! 
} 
var customerName: String? = getCustomer() 
println("name: (customerName?.uppercaseString)") 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Pattern Matching 
switch milesFlown["Jeff"]! { 
case 0..<25000: 
println("Jeff is a Premier member") 
case 25000..<50000: 
println("Jeff is a Silver member") 
case 50000..<75000: 
println("Jeff is a Gold member") 
case 75000..<100000: 
println("Jeff is a Platinum member") 
default: 
println("Jeff is an Elite member") 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Pattern Matching 
switch reservation.fareClass { 
case "F", "A", "J", "C": 
milesBonus = 2.5 // business/first 
case "Y", "B": 
milesBonus = 2.0 // full-fare economy 
case "M", "H": 
milesBonus = 1.5 // top-tier discount economy 
case let fc where fc.hasPrefix("Z"): 
milesBonus = 0 // upgrades don't earn miles 
default: 
milesBonus = 1.0 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Functions and Tuples 
func square(x: Int) -> Int { return x * x } 
func powers(x: Int) -> (Int, Int, Int) { 
return (x, x*x, x*x*x) 
} 
// The first tuple element is ignored 
let (_, square, cube) = powers(42) 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Closures 
func countIf(arr: [Int], pred: (Int) -> Bool) { 
var count = 0 
for n in arr { if pred(n) { ++count } } 
return count 
} 
countIf([1, 2, 3, 4], { n in n % 2 == 0 }) 
var squares = [17, 3, 11, 5, 7].map({ x in x * x }) 
sort(&squares, { a, b in a > b }) 
sort(&squares) { a, b in a > b } 
sort(&squares) { $0 > $1 } 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Swift REPL 
DEMO 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Classes 
class FrequentFlier { 
let name: String 
var miles: Double = 0 
init(name: String) { 
self.name = name 
} 
func addMileage(fareClass: String, dist: Int) { 
miles += bonus(fareClass) * Double(dist) 
} 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Custom Properties and 
Observers 
class MileageStatus { 
var miles: Double = 0.0 { 
willSet { 
assert(newValue >= miles) 
// can't decrease one's mileage 
} 
} 
var status: String { 
get { switch miles { /* ... */ } } 
} 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Smart Enums 
enum MemberLevel { 
case Silver 
case Gold 
case Platinum 
func milesRequired() -> Int { 
switch self { 
case .Silver: return 25000 
case .Gold: return 50000 
case .Platinum: return 75000 
} 
} 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Stateful Enums 
enum HttpResponse { 
case Success(body: String) 
case Error(description: String, statusCode: Int) 
} 
switch fakeHttpRequest("example.org") { 
case let .Error(desc, status) where status >= 500: 
println("internal server error: (desc)") 
case let .Error(desc, status): 
println("error (status) = (desc)") 
case .Success(let body): 
println("success, body = (body)") 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Extensions 
extension Int { 
var absoluteValue: Int { 
get { return abs(self) } 
} 
func times(action: () -> Void) { 
for _ in 1...self { 
action() 
} 
} 
} 
5.times { println("hello") } 
println((-5).absoluteValue) 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Generics 
class Queue<T> { 
var items = [T]() 
var depth: Int { 
get { return items.count } 
} 
func enqueue(item: T) { 
items.append(item) 
} 
func dequeue() -> T { 
return items.removeAtIndex(0) 
} 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Operators 
struct Complex { 
var r: Double, i: Double 
func add(z: Complex) -> Complex { 
return Complex(r: r+z.r, i: i+z.i) 
} 
} 
@infix func +(z1: Complex, z2: Complex) -> Complex { 
return z1.add(z2) 
} 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Custom Operators (Oh My!) 
operator prefix &*^%!~ {} 
@prefix func &*^%!~(s: String) -> String { 
return s.uppercaseString 
} 
operator infix ^^ { associativity left } 
@infix func ^^(a: Double, b: Double) -> Double { 
return pow(a, b) 
} 
println(&*^%!~"hello") // prints "HELLO" 
println(2.0^^4.0) // prints 8.0 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Swift and Objective-C 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Building an iOS app with Swift 
DEMO 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Wrapping Up 
• Most iOS developers are extremely 
excited about Swift 
• Swift has a much smoother learning curve 
and fixes many Objective-C mistakes 
• For the foreseeable future though, we will 
still use both languages 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift
Sasha Goldshtein 
blog.sashag.net 
@goldshtn 
s.sashag.net/sa-swift 
Thank You! 
Join the conversation on Twitter: @SoftArchConf #SA2014 
This presentation: http://s.sashag.net/sa-swift

More Related Content

PDF
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
PDF
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
PPTX
Numba-compiled Python UDFs for Impala (Impala Meetup 5/20/14)
PDF
Compiled Python UDFs for Impala
PPTX
Virtual machines - how they work
PDF
lldb – Debugger auf Abwegen
PDF
TVM VTA (TSIM)
PDF
Secure Programming Practices in C++ (NDC Security 2018)
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
Numba-compiled Python UDFs for Impala (Impala Meetup 5/20/14)
Compiled Python UDFs for Impala
Virtual machines - how they work
lldb – Debugger auf Abwegen
TVM VTA (TSIM)
Secure Programming Practices in C++ (NDC Security 2018)

What's hot (20)

PDF
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
PDF
Global Interpreter Lock: Episode I - Break the Seal
PDF
Trying to learn C# (NDC Oslo 2019)
PDF
Diving into HHVM Extensions (Brno PHP Conference 2015)
PDF
Model Serving via Pulsar Functions
PDF
A CTF Hackers Toolbox
PDF
Reliability Patterns for Fun and Profit
PDF
HHVM on AArch64 - BUD17-400K1
DOCX
Computer Networks Lab File
DOC
35787646 system-software-lab-manual
PDF
Kamil witecki asynchronous, yet readable, code
PDF
Lego: A brick system build by scala
PPT
2016年のPerl (Long version)
PPTX
{{components deepDive=true}}
DOC
Network lab manual
DOCX
DOCX
Network lap pgms 7th semester
PDF
Getting Started with Raspberry Pi - DCC 2013.1
PPTX
(Slightly) Smarter Smart Pointers
ODP
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
No instrumentation Golang Logging with eBPF (GoSF talk 11/11/20)
Global Interpreter Lock: Episode I - Break the Seal
Trying to learn C# (NDC Oslo 2019)
Diving into HHVM Extensions (Brno PHP Conference 2015)
Model Serving via Pulsar Functions
A CTF Hackers Toolbox
Reliability Patterns for Fun and Profit
HHVM on AArch64 - BUD17-400K1
Computer Networks Lab File
35787646 system-software-lab-manual
Kamil witecki asynchronous, yet readable, code
Lego: A brick system build by scala
2016年のPerl (Long version)
{{components deepDive=true}}
Network lab manual
Network lap pgms 7th semester
Getting Started with Raspberry Pi - DCC 2013.1
(Slightly) Smarter Smart Pointers
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Ad

Similar to Swift: Apple's New Programming Language for iOS and OS X (20)

PDF
Brand New JavaScript - ECMAScript 2015
PDF
.NET Fest 2018. Антон Молдован. One year of using F# in production at SBTech
ODP
Lift Presentation at DuSE VI
PDF
Best practices for crafting high quality PHP apps (php[world] 2019)
PDF
Adopting F# at SBTech
ZIP
Introduction to iPhone Development
ZIP
Introduction to iPhone Development
PDF
Akka http 2
PDF
Rack Middleware
PDF
The things we don't see – stories of Software, Scala and Akka
PPT
CppTutorial.ppt
PDF
Emerging Languages: A Tour of the Horizon
PPTX
Tuga IT 2018 Summer Edition - The Future of C#
PDF
Xitrum @ Scala Matsuri Tokyo 2014
PDF
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
PDF
JSON and the APInauts
PPTX
NetPonto - The Future Of C# - NetConf Edition
PDF
Best practices for crafting high quality PHP apps (Bulgaria 2019)
PPT
Cpp tutorial
PDF
Introduction to Scala for JCConf Taiwan
Brand New JavaScript - ECMAScript 2015
.NET Fest 2018. Антон Молдован. One year of using F# in production at SBTech
Lift Presentation at DuSE VI
Best practices for crafting high quality PHP apps (php[world] 2019)
Adopting F# at SBTech
Introduction to iPhone Development
Introduction to iPhone Development
Akka http 2
Rack Middleware
The things we don't see – stories of Software, Scala and Akka
CppTutorial.ppt
Emerging Languages: A Tour of the Horizon
Tuga IT 2018 Summer Edition - The Future of C#
Xitrum @ Scala Matsuri Tokyo 2014
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
JSON and the APInauts
NetPonto - The Future Of C# - NetConf Edition
Best practices for crafting high quality PHP apps (Bulgaria 2019)
Cpp tutorial
Introduction to Scala for JCConf Taiwan
Ad

More from Sasha Goldshtein (20)

PPTX
Modern Linux Tracing Landscape
PPTX
The Next Linux Superpower: eBPF Primer
PPTX
Staring into the eBPF Abyss
PPTX
Visual Studio 2015 and the Next .NET Framework
PPT
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
PPT
Modern Backends for Mobile Apps
PPT
.NET Debugging Workshop
PPT
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
PPT
Mastering IntelliTrace in Development and Production
PPTX
Introduction to RavenDB
PPTX
State of the Platforms
PPTX
Delivering Millions of Push Notifications in Minutes
PPTX
Building Mobile Apps with a Mobile Services .NET Backend
PPTX
Building iOS and Android Apps with Mobile Services
PPT
Task and Data Parallelism
PPT
What's New in C++ 11?
PDF
Attacking Web Applications
PPTX
Windows Azure Mobile Services
PPTX
First Steps in Android Development
PPTX
First Steps in iOS Development
Modern Linux Tracing Landscape
The Next Linux Superpower: eBPF Primer
Staring into the eBPF Abyss
Visual Studio 2015 and the Next .NET Framework
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
Modern Backends for Mobile Apps
.NET Debugging Workshop
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Mastering IntelliTrace in Development and Production
Introduction to RavenDB
State of the Platforms
Delivering Millions of Push Notifications in Minutes
Building Mobile Apps with a Mobile Services .NET Backend
Building iOS and Android Apps with Mobile Services
Task and Data Parallelism
What's New in C++ 11?
Attacking Web Applications
Windows Azure Mobile Services
First Steps in Android Development
First Steps in iOS Development

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
KodekX | Application Modernization Development
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Approach and Philosophy of On baking technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
The AUB Centre for AI in Media Proposal.docx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Digital-Transformation-Roadmap-for-Companies.pptx
Network Security Unit 5.pdf for BCA BBA.
Unlocking AI with Model Context Protocol (MCP)
Empathic Computing: Creating Shared Understanding
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
KodekX | Application Modernization Development
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
“AI and Expert System Decision Support & Business Intelligence Systems”
20250228 LYD VKU AI Blended-Learning.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Understanding_Digital_Forensics_Presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Approach and Philosophy of On baking technology

Swift: Apple's New Programming Language for iOS and OS X

  • 1. Swift Apple’s New Programming Language Sasha Goldshtein CTO, Sela Group blog.sashag.net @goldshtn Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 2. Apple WWDC Announcements • iOS 8 and OS X Yosemite • Xcode 6 • Swift Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 3. Minus Bazillion Points “Every feature starts out in the hole by 100 points, which means that it has to have a significant net positive effect on the overall package for it to make it into the language.” – Eric Gunnerson, C# compiler team If a language feature starts at -100 points, where does a new language start?! Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 4. Objective-C Popularity • Objective-C owes all its popularity to the meteoric rise of the iPhone App Store introduced Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 5. What’s Wrong with Objective-C Exhibit One // Ignore the horrible syntax for a second. // But here's the charmer: // 1) if 'str' is null, we return NSNotFound (232-1) // 2) if 'arr' is null, we return 0 + (NSUInteger)indexOfString:(NSString *)str inArray:(NSArray *)arr { return [arr indexOfObject:str]; } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 6. What’s Wrong with Objective-C Exhibit Two // This compiles and crashes at runtime with: // "-[__NSArrayI addObject:]: unrecognized selector // sent to instance 0x7a3141c0" NSMutableArray *array = [NSArray array]; [array addObject:@"hello"]; Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 7. What’s Wrong with Objective-C Exhibit Three NSMutableArray *array = [NSMutableArray array]; [array addObject:@"hello"]; [array addObject:@17]; // This compiles just fine NSString *string = array[1]; // Yes, the "17" // This works and prints 17 NSLog(@"%@", string); // But this crashes NSLog(@"%@", [string stringByAppendingString:@"hmm"]); Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 8. Swift Language Principles • Clean and modern • Type-safe and generic • Extensible • Functional • Productive • Compiled to native • Seamlessly interops with Objective-C Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 9. Why Not an Existing Language? • Good philosophical question • If you want some more philosophy: http://s.sashag.net/why-swift We will now review some of the interesting language decisions, which make Swift different from mainstream languages Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 10. Variables and Constants let size = 42 let name = "Dave" var address = "14 Franklin Way" address = "16 Calgary Drive" var city: String city = "London" var pi: Double = 3.14 Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 11. Strings and Arrays var fullAddress = city + " " + address println("(name)'s at (city.capitalizedString)") var customers = ["Dave", "Kate", "Jack"] var addresses = [String]() addresses += fullAddress if customers[2].hasPrefix("Ja") { customers.removeAtIndex(2) } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 12. Dictionaries var orderSummary = [ "Dave": 11000, "Kate": 14000, "Jack": 13500 ] orderSummary["Kate"] = 17500 for (customer, orders) in orderSummary { println("(customer) -> (orders)") } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 13. Null “I call it my billion-dollar mistake. It was the invention of the null reference in 1965. […] I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.” – Sir Charles Antony Richard Hoare, 2009 Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 14. Optional Types var myName: String myName = nil // Does not compile! var jeffsOrders: Int? jeffsOrders = orderSummary["Jeff”] if jeffsOrders { let orders: Int = jeffsOrders! } var customerName: String? = getCustomer() println("name: (customerName?.uppercaseString)") Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 15. Pattern Matching switch milesFlown["Jeff"]! { case 0..<25000: println("Jeff is a Premier member") case 25000..<50000: println("Jeff is a Silver member") case 50000..<75000: println("Jeff is a Gold member") case 75000..<100000: println("Jeff is a Platinum member") default: println("Jeff is an Elite member") } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 16. Pattern Matching switch reservation.fareClass { case "F", "A", "J", "C": milesBonus = 2.5 // business/first case "Y", "B": milesBonus = 2.0 // full-fare economy case "M", "H": milesBonus = 1.5 // top-tier discount economy case let fc where fc.hasPrefix("Z"): milesBonus = 0 // upgrades don't earn miles default: milesBonus = 1.0 } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 17. Functions and Tuples func square(x: Int) -> Int { return x * x } func powers(x: Int) -> (Int, Int, Int) { return (x, x*x, x*x*x) } // The first tuple element is ignored let (_, square, cube) = powers(42) Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 18. Closures func countIf(arr: [Int], pred: (Int) -> Bool) { var count = 0 for n in arr { if pred(n) { ++count } } return count } countIf([1, 2, 3, 4], { n in n % 2 == 0 }) var squares = [17, 3, 11, 5, 7].map({ x in x * x }) sort(&squares, { a, b in a > b }) sort(&squares) { a, b in a > b } sort(&squares) { $0 > $1 } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 19. Swift REPL DEMO Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 20. Classes class FrequentFlier { let name: String var miles: Double = 0 init(name: String) { self.name = name } func addMileage(fareClass: String, dist: Int) { miles += bonus(fareClass) * Double(dist) } } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 21. Custom Properties and Observers class MileageStatus { var miles: Double = 0.0 { willSet { assert(newValue >= miles) // can't decrease one's mileage } } var status: String { get { switch miles { /* ... */ } } } } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 22. Smart Enums enum MemberLevel { case Silver case Gold case Platinum func milesRequired() -> Int { switch self { case .Silver: return 25000 case .Gold: return 50000 case .Platinum: return 75000 } } } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 23. Stateful Enums enum HttpResponse { case Success(body: String) case Error(description: String, statusCode: Int) } switch fakeHttpRequest("example.org") { case let .Error(desc, status) where status >= 500: println("internal server error: (desc)") case let .Error(desc, status): println("error (status) = (desc)") case .Success(let body): println("success, body = (body)") } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 24. Extensions extension Int { var absoluteValue: Int { get { return abs(self) } } func times(action: () -> Void) { for _ in 1...self { action() } } } 5.times { println("hello") } println((-5).absoluteValue) Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 25. Generics class Queue<T> { var items = [T]() var depth: Int { get { return items.count } } func enqueue(item: T) { items.append(item) } func dequeue() -> T { return items.removeAtIndex(0) } } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 26. Operators struct Complex { var r: Double, i: Double func add(z: Complex) -> Complex { return Complex(r: r+z.r, i: i+z.i) } } @infix func +(z1: Complex, z2: Complex) -> Complex { return z1.add(z2) } Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 27. Custom Operators (Oh My!) operator prefix &*^%!~ {} @prefix func &*^%!~(s: String) -> String { return s.uppercaseString } operator infix ^^ { associativity left } @infix func ^^(a: Double, b: Double) -> Double { return pow(a, b) } println(&*^%!~"hello") // prints "HELLO" println(2.0^^4.0) // prints 8.0 Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 28. Swift and Objective-C Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 29. Building an iOS app with Swift DEMO Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 30. Wrapping Up • Most iOS developers are extremely excited about Swift • Swift has a much smoother learning curve and fixes many Objective-C mistakes • For the foreseeable future though, we will still use both languages Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift
  • 31. Sasha Goldshtein blog.sashag.net @goldshtn s.sashag.net/sa-swift Thank You! Join the conversation on Twitter: @SoftArchConf #SA2014 This presentation: http://s.sashag.net/sa-swift

Editor's Notes

  • #4: Quote source: http://blogs.msdn.com/b/ericgu/archive/2004/01/12/57985.aspx