Last active
January 28, 2016 09:39
-
-
Save AsceticMonk/66c314338e2c6cfe4a45 to your computer and use it in GitHub Desktop.
Swift 2.0 language changes
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.") | |
for (index, value) in languages.enumerate() { | |
print("\(index). \(value)") | |
} | |
// Global function equal is replaced by simple equality operator | |
let easternLanguages = ["Chinese", "Japanese", "Korean", "Thai"] | |
if languages == easternLanguages { | |
print("Same set of languages") | |
} else { | |
print("Different sets") | |
} | |
languages.extend(["Dutch"]) | |
for lang in languages.filter({ $0 != "Chinese"}) { | |
print(lang) | |
} | |
// Instead of global function find, we have method indexOf | |
languages.indexOf("Chinese") | |
for item in languages.map({ "- " + $0 }) { | |
print(item) | |
} | |
let languageString = languages.reduce("") { $0 + $1 } | |
languageString | |
languages.sort() // equivalent to the old global function sorted | |
languages.sortInPlace() // equivalent to the old global function sort | |
languages.startsWith(["Japanese"]) | |
easternLanguages.startsWith(["Chinese"]) | |
/* | |
String Character Count | |
*/ | |
let greeting = "Hello my dear friend!" | |
// Swift 1.0 | |
//countElement(greeting) | |
// Swift 1.2 | |
//count(greeting) | |
// Swift 2.0 | |
greeting.characters.count | |
/* | |
Do-While Gone, Welcome Repeat-While | |
*/ | |
var counter = 3 | |
repeat { | |
print("Go Chargers!") | |
counter-- | |
} while counter > 0 | |
/* | |
New Error Handling | |
*/ | |
enum InvalidLanguageSetError: ErrorType { | |
case EnglishNotIncludedError | |
} | |
func verifyLanguageSet(languages: [String]) throws { | |
if !languages.contains("English") { | |
throw InvalidLanguageSetError.EnglishNotIncludedError | |
} | |
} | |
do { | |
try verifyLanguageSet(easternLanguages) | |
print("Verified!") | |
} catch { | |
print("No English in the set!") | |
} | |
/* | |
New Keyword guard | |
Note: Wondering why would you use guard? Read this, http://ericcerney.com/swift-guard-statement/ | |
*/ | |
func greetUser(user: String?) { | |
guard let username = user where username.characters.count > 0 else { return } | |
print("Hello \(username)!") | |
} | |
var user: String? | |
greetUser(user) | |
user = "" | |
greetUser(user) | |
user = "George" | |
greetUser(user) | |
/* | |
Other notable changes: | |
- Availability #available | |
- No more println, just print | |
- No more toString, use String initializer which takes pretty much any type | |
- New keyword defer | |
Online resource about Swift 2.0: | |
- https://developer.apple.com/swift/blog/?id=29 | |
- http://www.raywenderlich.com/108522/whats-new-in-swift-2 | |
- https://www.youtube.com/watch?v=pHsvYPMqsOc | |
- http://airspeedvelocity.net/2015/06/09/changes-to-the-swift-standard-library-in-2-0-beta-1/ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment