Skip to content

Instantly share code, notes, and snippets.

@m4scosta
Created December 3, 2015 00:06
Show Gist options
  • Save m4scosta/e5b733370ae15d90f099 to your computer and use it in GitHub Desktop.
Save m4scosta/e5b733370ae15d90f099 to your computer and use it in GitHub Desktop.
// String operations
var test = "Marcos Pinto"
test.insert(" ", atIndex: test.startIndex.advancedBy(6))
test.insertContentsOf("Costa".characters, at: test.startIndex.advancedBy(7))
print(test)
test.removeAtIndex(test.startIndex)
print(test)
test.removeRange(test.startIndex...test.startIndex.advancedBy(5))
print(test)
print(test.hasPrefix(" Costa"))
print(test.hasSuffix("Pinto"))
// if let ...
let b: Int? = 1
if let a = b {
print(a)
}
// switch clausules
let ages = (14, 15)
switch ages {
case (_, 15): // whitecard (_)
print("OK1")
case (14, _):
print("OK2")
case (_, let y) where y > 10: // let, where
print("OK3 \(y)")
fallthrough
default:
print("default")
}
// labeled statements
mainFor: for i in 1...10 {
subFor: for j in 1...10 {
print("\(i) \(j)")
if (i + j) % 3 == 0 {
continue mainFor
}
}
}
// early exiting
func exitEarly() {
return
print("OK")
}
func exitEarly2(name: String?) {
guard let n = name else {
print("nil passed")
return
}
print("name is \(n)")
}
exitEarly()
exitEarly2("Marcos")
exitEarly2(nil)
// platform availability
if #available(OSX 10.10, *) {
print("OK")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment