Skip to content

Instantly share code, notes, and snippets.

@tikidunpon
Created September 20, 2016 12:34
Show Gist options
  • Save tikidunpon/2ec27e13d4c64c9db40aa52665adabc3 to your computer and use it in GitHub Desktop.
Save tikidunpon/2ec27e13d4c64c9db40aa52665adabc3 to your computer and use it in GitHub Desktop.
シャドーイングの話 #CodePiece #minna_de_swift
// シャドーイング
let value: Optional<Int> = 10
// 同名の変数の定義で元の変数へのアクセスを制限する機能
// この例ではOptional Binding
if let value = value {
print(1 + value)
}
// シャドーイングがない言語の場合はこうなる?
if let unwrappedValue = value {
print(1 + unwrappedValue)
}
// これはシャドーイング起きない
let value2: Optional<Int> = 20
guard let v = value2 else {
fatalError()
}
print(v)
print(value2)
let value3: Optional<Int> = 30
// switchはdefault書かなければ網羅性があるよねーという話
switch value3 {
case let value3?:
print(value3 + 1)
case nil:
//fatalError()より、
//assertionFailure()の方がリリース時に無効になるのでオススメ
assertionFailure()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment