SlideShare a Scribd company logo
SWIFT
by Michael Yagudaev
@yagudaev
Outline
• Swift REPL
• Alcatraz (no not the island!)
• Type Safety
• Extendability
• Managing External Dependencies
• Obj-C to Swift and Vice Versa
• Structuring your application
Who Am I
• Rails/Mobile Developer @ Gastown Labs
• Fitness Wearable fanatic…
• Entrepreneur, side project: FitnessKPI
Online Resources
• Swift is very young, there are a few good
resources - just need to find them
NSHipster - posts about iOS Dev and Swift
NSScreencast - similar to railcasts
CodeSchool - Interactive
course to learn Obj-C
Swift REPL
• Run Swift Code from Terminal
• No need to load xCode! (YAY!)
• Similar to IRB, doesn’t have access to application
context
• see the `lldb` expression commands
• (lldb) expr myVar.val()
• http://lldb.llvm.org/lldb-gdb.html
Alcatraz
• xCode sucks. We all know it.
• Alcatraz is a package manager for xCode
(similar to sublime’s package manager)
• Be a hipster, get a dark theme! I use `Sidewalk
Chalk`
Type Safety
• Swift is strongly typed and extremely type safe
• Optionals are variables that can hold a nil value
• In swift, all variables and constants must have a
value, unless they are optional
let PI:Double = 3.14
var twoPI = PI * 2
var sum:Double? = nil
sum = 0
println(sum) // 0
sum = sum + 1 // error! sum is optional
sum = sum! + 1 // works! unwraps optional before addition
var sum:Double = nil // error Double cannot have a nil value!
Type Safety (Cont’d)
• Nil checking is more expressive in Swift
• Chaining optional is a good way to simplify code
• Similar to how things work with CoffeeScript
person.car?.doors[0]?.open()
Extendability
• adding toArray to Range
extension Range {
func toArray() -> [T] {
return [T](self)
}
}
(0...500).toArray()
class Range
def to_array
self.to_a
end
end
(0…500).to_array()
Swift
Ruby
Extendability (cont’d)
• Union of two dictionaries
func + <K,V>(left: Dictionary<K,V>, right: Dictionary<K,V>) -> Dictionary<K,V>
{
var map = Dictionary<K,V>()
for (k, v) in left {
map[k] = v
}
for (k, v) in right {
map[k] = v
}
return map
}
let ab = ["a": 1] + ["b": 2]
class Hash
def +(right)
hash = {}
self.each { |k,v| hash[k] = v }
right.each { |k,v| hash[k] = v }
hash
end
end
ab = {a: 1} + {b: 2}
Swift
Ruby
Extendability WARNING!
PLEASE EXTEND RESPONSIBLY!
Fun Fact
• Swift supports unicode characters for variable
names… (i.e. prank friends using google
translate).
let акулажир = "Shark Fat"
let 瘦鲨⻥鱼 = "skinny Shark"
Managing External
Dependencies
• Most modern languages have the notion of a
package manager. e.g. NPM, Ruby Gems, Bower
• In swift we have Cocoapods… or do we?
• Can only use Cocoapods with Obj-C code (for
now)… :(
• http://www.swifttoolbox.io/ - 

great collection of swift 

packages
Accessing Obj-C from Swift
• Really easy, only need header bridge.h file and
you are set.
“Objective-C is done fool!”
Not quite…
How Obj-C interfaces
Translate to Swift
• Initializers
• Enums
• Unions???
UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero];
let myTableView = UITableView(frame:CGRectZero)
UIButtonType button_type = UIButtonTypeDetailDisclosure;
var buttonType = UIButtonType.DetailDisclosure
How Obj-C interfaces
Translate to Swift (cont’d)
• Unions Obj-C Wrapper
@interface GyroData: NSObject
@property (nonatomic) float x;
@property (nonatomic) float y;
@property (nonatomic) float z;
@end
@implementation GyroData
@end
+ (GyroData *) getGyro:(TLMGyroscopeEvent *)gyroEvent {
GLKVector3 vector = gyroEvent.vector;
GyroData *result = [GyroData new];
result.x = vector.x;
result.y = vector.y;
result.z = vector.z;
return result;
}
let gyroData = GLKitPolyfill.getGyro(gyroEvent)
swift
Accessing Swift Code from
Obj-C
• More difficult, mostly don’t need to do it…
• Uses automatically generated header file
• Add @objc tag to classes and other interfaces
you want to expose to Objective C code.
(CoreData needs this)
Structuring Applications
• No strict directory
structure to iOS apps
• No concept of “folders”
but “groups”, need to
manually keep in sync
• Use the best practices
you learned from Rails
Persistence
• CoreData - if your hardcore like that
• MagicalRecord - ORM for CoreData
• Realm - replacement for CoreData, cross
platform and easier workflow
References
• https://signalvnoise.com/posts/3743
• https://signalvnoise.com/posts/3432-why-i-loved-
building-basecamp-for-iphone-in-rubymotion
• http://realm.io/news/swift-for-rubyists/
• http://www.slideshare.net/josephku/swift-introduction-
to-swift-in-ruby
• http://www.bignerdranch.com/blog/discover-swift-with-
this-one-weird-rubyist/
• http://blog.thefrontiergroup.com.au/2014/09/should-
my-company-choose-rubymotion-or-swift/
• Swift (book)
• Using Swift with Cocoa and Objective-C
QUESTIONS?

More Related Content

PDF
LINQ Inside
PDF
F#语言对异步程序设计的支持
PDF
WDB005.1 - JavaScript for Java Developers (Lecture 1)
PDF
使用.NET构建轻量级分布式框架
PDF
Working with Cocoa and Objective-C
PDF
JavaScript For CSharp Developer
KEY
The JavaScript Programming Primer
PDF
TypeScript Introduction
LINQ Inside
F#语言对异步程序设计的支持
WDB005.1 - JavaScript for Java Developers (Lecture 1)
使用.NET构建轻量级分布式框架
Working with Cocoa and Objective-C
JavaScript For CSharp Developer
The JavaScript Programming Primer
TypeScript Introduction

What's hot (20)

PDF
Angular 2.0: Brighter future?
PDF
SWIFT1 Optional
PDF
JavaScript: Patterns, Part 1
ODP
JavaScript global object, execution contexts & closures
PDF
JavaScript Execution Context
PPTX
Javascript ES6
PDF
SE 20016 - programming languages landscape.
PDF
Scala e JRuby
PDF
Swift in SwiftUI
PDF
Swift core
PPTX
Javascript asynchronous
PPTX
OOP in Scala
PDF
RubyMotion Introduction
PPTX
Connecting C++ and JavaScript on the Web with Embind
PDF
Why scala is not my ideal language and what I can do with this
PPTX
Introduction to Kotlin Language and its application to Android platform
PDF
TRICK2015 results
PPTX
C# Today and Tomorrow
ZIP
拡張ライブラリをD言語で作るとリア充
PDF
Typescript is the best by Maxim Kryuk
Angular 2.0: Brighter future?
SWIFT1 Optional
JavaScript: Patterns, Part 1
JavaScript global object, execution contexts & closures
JavaScript Execution Context
Javascript ES6
SE 20016 - programming languages landscape.
Scala e JRuby
Swift in SwiftUI
Swift core
Javascript asynchronous
OOP in Scala
RubyMotion Introduction
Connecting C++ and JavaScript on the Web with Embind
Why scala is not my ideal language and what I can do with this
Introduction to Kotlin Language and its application to Android platform
TRICK2015 results
C# Today and Tomorrow
拡張ライブラリをD言語で作るとリア充
Typescript is the best by Maxim Kryuk
Ad

Viewers also liked (10)

PDF
Photography for Equality // Phfe guidelines
PPT
Vanjs backbone-powerpoint
PPTX
Generic Programming &amp; Collection
PPTX
Swift Programming Language Presentation
PDF
Functional Programming 之二三事
PDF
Open Source Creativity
PPSX
Reuters: Pictures of the Year 2016 (Part 2)
PDF
The impact of innovation on travel and tourism industries (World Travel Marke...
PDF
The Outcome Economy
PDF
The Six Highest Performing B2B Blog Post Formats
Photography for Equality // Phfe guidelines
Vanjs backbone-powerpoint
Generic Programming &amp; Collection
Swift Programming Language Presentation
Functional Programming 之二三事
Open Source Creativity
Reuters: Pictures of the Year 2016 (Part 2)
The impact of innovation on travel and tourism industries (World Travel Marke...
The Outcome Economy
The Six Highest Performing B2B Blog Post Formats
Ad

Similar to Swift for-rubyists (20)

PDF
Swift, swiftly
PDF
Swift, a quick overview
PDF
To Swift 2...and Beyond!
PDF
Swiftly Switching: The Transition from Obj-C to Swift
PPTX
Swift vs Objective-C
PDF
Swift Tutorial Part 2. The complete guide for Swift programming language
PDF
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
PDF
Intro toswift1
PDF
iOS NSAgora #3: Objective-C vs. Swift
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 02)
PDF
Денис Лебедев, Swift
PDF
Swift rocks! #1
PDF
Swift Basics
PDF
Denis Lebedev, Swift
PDF
Objective-C to Swift - Swift Cloud Workshop 3
PDF
CocoaHeads Bratislava #1 - Swift
PDF
Swift, functional programming, and the future of Objective-C
PDF
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
PDF
Swift after one week of coding
Swift, swiftly
Swift, a quick overview
To Swift 2...and Beyond!
Swiftly Switching: The Transition from Obj-C to Swift
Swift vs Objective-C
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Intro toswift1
iOS NSAgora #3: Objective-C vs. Swift
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 02)
Денис Лебедев, Swift
Swift rocks! #1
Swift Basics
Denis Lebedev, Swift
Objective-C to Swift - Swift Cloud Workshop 3
CocoaHeads Bratislava #1 - Swift
Swift, functional programming, and the future of Objective-C
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Swift after one week of coding

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Cloud computing and distributed systems.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Digital-Transformation-Roadmap-for-Companies.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
NewMind AI Weekly Chronicles - August'25 Week I
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Cloud computing and distributed systems.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Swift for-rubyists

  • 2. Outline • Swift REPL • Alcatraz (no not the island!) • Type Safety • Extendability • Managing External Dependencies • Obj-C to Swift and Vice Versa • Structuring your application
  • 3. Who Am I • Rails/Mobile Developer @ Gastown Labs • Fitness Wearable fanatic… • Entrepreneur, side project: FitnessKPI
  • 4. Online Resources • Swift is very young, there are a few good resources - just need to find them NSHipster - posts about iOS Dev and Swift NSScreencast - similar to railcasts CodeSchool - Interactive course to learn Obj-C
  • 5. Swift REPL • Run Swift Code from Terminal • No need to load xCode! (YAY!) • Similar to IRB, doesn’t have access to application context • see the `lldb` expression commands • (lldb) expr myVar.val() • http://lldb.llvm.org/lldb-gdb.html
  • 6. Alcatraz • xCode sucks. We all know it. • Alcatraz is a package manager for xCode (similar to sublime’s package manager) • Be a hipster, get a dark theme! I use `Sidewalk Chalk`
  • 7. Type Safety • Swift is strongly typed and extremely type safe • Optionals are variables that can hold a nil value • In swift, all variables and constants must have a value, unless they are optional let PI:Double = 3.14 var twoPI = PI * 2 var sum:Double? = nil sum = 0 println(sum) // 0 sum = sum + 1 // error! sum is optional sum = sum! + 1 // works! unwraps optional before addition var sum:Double = nil // error Double cannot have a nil value!
  • 8. Type Safety (Cont’d) • Nil checking is more expressive in Swift • Chaining optional is a good way to simplify code • Similar to how things work with CoffeeScript person.car?.doors[0]?.open()
  • 9. Extendability • adding toArray to Range extension Range { func toArray() -> [T] { return [T](self) } } (0...500).toArray() class Range def to_array self.to_a end end (0…500).to_array() Swift Ruby
  • 10. Extendability (cont’d) • Union of two dictionaries func + <K,V>(left: Dictionary<K,V>, right: Dictionary<K,V>) -> Dictionary<K,V> { var map = Dictionary<K,V>() for (k, v) in left { map[k] = v } for (k, v) in right { map[k] = v } return map } let ab = ["a": 1] + ["b": 2] class Hash def +(right) hash = {} self.each { |k,v| hash[k] = v } right.each { |k,v| hash[k] = v } hash end end ab = {a: 1} + {b: 2} Swift Ruby
  • 12. Fun Fact • Swift supports unicode characters for variable names… (i.e. prank friends using google translate). let акулажир = "Shark Fat" let 瘦鲨⻥鱼 = "skinny Shark"
  • 13. Managing External Dependencies • Most modern languages have the notion of a package manager. e.g. NPM, Ruby Gems, Bower • In swift we have Cocoapods… or do we? • Can only use Cocoapods with Obj-C code (for now)… :( • http://www.swifttoolbox.io/ - 
 great collection of swift 
 packages
  • 14. Accessing Obj-C from Swift • Really easy, only need header bridge.h file and you are set.
  • 15. “Objective-C is done fool!” Not quite…
  • 16. How Obj-C interfaces Translate to Swift • Initializers • Enums • Unions??? UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectZero]; let myTableView = UITableView(frame:CGRectZero) UIButtonType button_type = UIButtonTypeDetailDisclosure; var buttonType = UIButtonType.DetailDisclosure
  • 17. How Obj-C interfaces Translate to Swift (cont’d) • Unions Obj-C Wrapper @interface GyroData: NSObject @property (nonatomic) float x; @property (nonatomic) float y; @property (nonatomic) float z; @end @implementation GyroData @end + (GyroData *) getGyro:(TLMGyroscopeEvent *)gyroEvent { GLKVector3 vector = gyroEvent.vector; GyroData *result = [GyroData new]; result.x = vector.x; result.y = vector.y; result.z = vector.z; return result; } let gyroData = GLKitPolyfill.getGyro(gyroEvent) swift
  • 18. Accessing Swift Code from Obj-C • More difficult, mostly don’t need to do it… • Uses automatically generated header file • Add @objc tag to classes and other interfaces you want to expose to Objective C code. (CoreData needs this)
  • 19. Structuring Applications • No strict directory structure to iOS apps • No concept of “folders” but “groups”, need to manually keep in sync • Use the best practices you learned from Rails
  • 20. Persistence • CoreData - if your hardcore like that • MagicalRecord - ORM for CoreData • Realm - replacement for CoreData, cross platform and easier workflow
  • 21. References • https://signalvnoise.com/posts/3743 • https://signalvnoise.com/posts/3432-why-i-loved- building-basecamp-for-iphone-in-rubymotion • http://realm.io/news/swift-for-rubyists/ • http://www.slideshare.net/josephku/swift-introduction- to-swift-in-ruby • http://www.bignerdranch.com/blog/discover-swift-with- this-one-weird-rubyist/ • http://blog.thefrontiergroup.com.au/2014/09/should- my-company-choose-rubymotion-or-swift/ • Swift (book) • Using Swift with Cocoa and Objective-C