Upload
michael-yagudaev
View
145
Download
3
Embed Size (px)
Citation preview
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?