This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Power of Protocol Extensions | |
Some global functions became methods | |
*/ | |
var languages = ["Spanish", "English", "Chinese", "Japanese"] | |
languages.contains("Italian") | |
print("We have \(languages.count) languages.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class FavoriteCity { | |
dynamic func currentWinner() -> String { | |
return "Vancouver" | |
} | |
dynamic func lastWinner() -> String { | |
return "Shanghai" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let formatter = NSNumberFormatter() | |
formatter.locale = NSLocale.currentLocale() | |
// Currency | |
formatter.numberStyle = .CurrencyStyle | |
formatter.stringFromNumber(49.99) | |
// Currency in German |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
// Do something on the main queue asynchronously | |
DispatchQueue.main.async { | |
print("Update UI") | |
} |