Swift for-rubyists

22
SWIFT by Michael Yagudaev @yagudaev

Transcript of Swift for-rubyists

Page 1: Swift for-rubyists

SWIFT

by Michael Yagudaev@yagudaev

Page 2: Swift for-rubyists

Outline• Swift REPL

• Alcatraz (no not the island!)

• Type Safety

• Extendability

• Managing External Dependencies

• Obj-C to Swift and Vice Versa

• Structuring your application

Page 3: Swift for-rubyists

Who Am I• Rails/Mobile Developer @ Gastown Labs

• Fitness Wearable fanatic…

• Entrepreneur, side project: FitnessKPI

Page 4: Swift for-rubyists

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

Page 5: Swift for-rubyists

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

Page 6: Swift for-rubyists

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`

Page 7: Swift for-rubyists

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!

Page 8: Swift for-rubyists

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()

Page 9: Swift for-rubyists

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

Page 10: Swift for-rubyists

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

Page 11: Swift for-rubyists

Extendability WARNING!

PLEASE EXTEND RESPONSIBLY!

Page 12: Swift for-rubyists

Fun Fact• Swift supports unicode characters for variable

names… (i.e. prank friends using google translate).

let акулажир = "Shark Fat"

let 瘦鲨⻥鱼 = "skinny Shark"

Page 13: Swift for-rubyists

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

Page 14: Swift for-rubyists

Accessing Obj-C from Swift• Really easy, only need header bridge.h file and

you are set.

Page 15: Swift for-rubyists

“Objective-C is done fool!”

Not quite…

Page 16: Swift for-rubyists

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

Page 17: Swift for-rubyists

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

Page 18: Swift for-rubyists

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)

Page 19: Swift for-rubyists

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

Page 20: Swift for-rubyists

Persistence• CoreData - if your hardcore like that

• MagicalRecord - ORM for CoreData

• Realm - replacement for CoreData, cross platform and easier workflow

Page 21: Swift for-rubyists

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

Page 22: Swift for-rubyists

QUESTIONS?