Skip to content

Instantly share code, notes, and snippets.

@yhkaplan
Created January 15, 2019 06:28
Show Gist options
  • Save yhkaplan/c23496d96a503ed7105da7eb57335609 to your computer and use it in GitHub Desktop.
Save yhkaplan/c23496d96a503ed7105da7eb57335609 to your computer and use it in GitHub Desktop.
// Swift safety
/// Type safety
let i: UInt8 = 1
let x: Int = 4
// Error
// let result = i*x
func superPrint(string: String) {
for _ in [0...999] { print(string) }
}
superPrint(string: "test")
let optionalString: String? = "I am optional"
// Error
//superPrint(string: optionalString)
// Optionals
enum Optional2<T> {
case some(T)
case none
}
let optionalInt: Optional<Int> = 3
let anotherOptionalInt: Int? = Optional.none
let yetAnotherOptionalInt = Optional.some(9)
// if let
if let someInt = optionalInt {
print(someInt)
}
// optional chaining
[1, 2, 3].first?.signum()
// guard
func test() {
guard let anInt = optionalInt else { return }
print(anInt)
}
// ?? nil coalescing
anotherOptionalInt ?? 9
// force unwrap
yetAnotherOptionalInt!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment