KEMBAR78
Swift for-rubyists | PDF
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?

Swift for-rubyists

  • 1.
  • 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 • Swiftis 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 • RunSwift 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 • Swiftis 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 toArrayto 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) • Unionof 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
  • 11.
  • 12.
    Fun Fact • Swiftsupports unicode characters for variable names… (i.e. prank friends using google translate). let акулажир = "Shark Fat" let 瘦鲨⻥鱼 = "skinny Shark"
  • 13.
    Managing External Dependencies • Mostmodern 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 fromSwift • Really easy, only need header bridge.h file and you are set.
  • 15.
    “Objective-C is donefool!” Not quite…
  • 16.
    How Obj-C interfaces Translateto 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 Translateto 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 Codefrom 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 • Nostrict 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
  • 22.