Skip to content

Instantly share code, notes, and snippets.

@AsceticMonk
AsceticMonk / Swift1Point2.swift
Last active January 28, 2016 09:39
Swift 1.2 langauge changes
import Foundation
/*
Improved Optional Binding: goodbye pyramid of doom
Code and note extracted from: http://nshipster.com/swift-1.2/ (slightly modified)
*/
// Before Swift 1.2
let a = "10".toInt()
let b = "5".toInt()
@AsceticMonk
AsceticMonk / Swift2Point0.swift
Last active January 28, 2016 09:39
Swift 2.0 language changes
/*
Power of Protocol Extensions
Some global functions became methods
*/
var languages = ["Spanish", "English", "Chinese", "Japanese"]
languages.contains("Italian")
print("We have \(languages.count) languages.")
@AsceticMonk
AsceticMonk / MethodSwizzling.swift
Created January 28, 2016 09:52
Swift instance method swizzling
import Foundation
class FavoriteCity {
dynamic func currentWinner() -> String {
return "Vancouver"
}
dynamic func lastWinner() -> String {
return "Shanghai"
}
@AsceticMonk
AsceticMonk / LocalizedNumbers.swift
Last active February 3, 2016 08:38
NSNumberFormatter basics
import Foundation
let formatter = NSNumberFormatter()
formatter.locale = NSLocale.currentLocale()
// Currency
formatter.numberStyle = .CurrencyStyle
formatter.stringFromNumber(49.99)
// Currency in German
@AsceticMonk
AsceticMonk / DateInterval.swift
Last active June 23, 2016 03:24
New value type in Foundation
import Foundation
// DateInterval, new value type
let interval1 = DateInterval()
interval1.duration
print("Same start and end date? \(interval1.start == interval1.end)") // True
// Creating a date interval between now and 1 month later
let interval2 = DateInterval(start: Date(), duration: TimeInterval(2_628_000))
interval2.duration
@AsceticMonk
AsceticMonk / Dispatch.swift
Last active December 16, 2016 20:23
New Swift 3.0 Dispatch APIs
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// Do something on the main queue asynchronously
DispatchQueue.main.async {
print("Update UI")
}