이번 스터디는 위의 링크에서 소개한 if let 구문을 보다 simple하게 만들어서 소개한 개발자의 글을 읽었는데 내용이 꽤 괜찮아서 본문을 읽고 정리해 보았다.
이 내용은 공식적인 swift의 문법이 아닌 것을 다시한번 알려 드린다. 위 링크를 꼭 참고해보라,
옵셔널을 빼주기 위해서 여러 방법들이 사용되는데, 그중 if let 구문을 알고 있을 것이다.
if let escapeOptional = escapeOptional {
...
}
지금까지 이런 구문으로 if let 구문을 써왔다.
let someLengthyVariableName: Foo? = ...
let anotherImportantVariable: Bar? = ...
if let someLengthyVariableName = someLengthyVariableName, let anotherImportantVariable = anotherImportantVariable {
...
}
위처럼 불필요한 이름을 추가로 만드는 것이 단점이었는데 이 부분을 개선하는 것이다.
let foo: Foo? = ...
if let foo {
// `foo` is of type `Foo`
}
이렇게 이제 업데이트가 되면 좀 더 편해질 것이다.
이제 아래와 같이 사용이 가능하다.
let someLengthyVariableName: Foo? = ...
let anotherImportantVariable: Bar? = ...
if let someLengthyVariableName, let anotherImportantVariable {
...
}
뿐만 아니라 이 업데이트는 옵셔널 바인딩 옵션으로 확장이 가능한데
현재 버전이 아래와 같다면