Skip to content

Instantly share code, notes, and snippets.

Avatar

George Yang AsceticMonk

View GitHub Profile
@AsceticMonk
AsceticMonk / Dispatch.swift
Last active Dec 16, 2016
New Swift 3.0 Dispatch APIs
View Dispatch.swift
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// Do something on the main queue asynchronously
DispatchQueue.main.async {
print("Update UI")
}
@AsceticMonk
AsceticMonk / DateInterval.swift
Last active Jun 23, 2016
New value type in Foundation
View DateInterval.swift
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 / LocalizedNumbers.swift
Last active Feb 3, 2016
NSNumberFormatter basics
View LocalizedNumbers.swift
import Foundation
let formatter = NSNumberFormatter()
formatter.locale = NSLocale.currentLocale()
// Currency
formatter.numberStyle = .CurrencyStyle
formatter.stringFromNumber(49.99)
// Currency in German
@AsceticMonk
AsceticMonk / MethodSwizzling.swift
Created Jan 28, 2016
Swift instance method swizzling
View MethodSwizzling.swift
import Foundation
class FavoriteCity {
dynamic func currentWinner() -> String {
return "Vancouver"
}
dynamic func lastWinner() -> String {
return "Shanghai"
}
@AsceticMonk
AsceticMonk / Swift2Point0.swift
Last active Jan 28, 2016
Swift 2.0 language changes
View Swift2Point0.swift
/*
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 / Swift1Point2.swift
Last active Jan 28, 2016
Swift 1.2 langauge changes
View Swift1Point2.swift
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()