Apple has a new programming language? It looks like ruby and I don't have to learn Objective C? Well, not quite...
Based on a true life story, VanSwift present a thrilling story of trial and tribulation. Many restless nights cursing at XCode, staring at the stars and wishing this was ruby -- a gem of a language. We will cover what you need to know to be an effective Swift programmer if you are coming from Ruby, Python or Javascript.
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.
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