선택적 바인딩을 사용하면 모든 논리 블록을 매번 같은 방식으로 사용할 수 있습니다. 선택 체인의 여러 행은 예상치 못한 동작을 일으킬 수있는 경쟁 조건에 잠재적으로 영향을받을 수있는 상황을 정확히 모호하게 만들 수 있습니다.
선택적 바인딩을 사용하면 방금 래핑하지 않은 항목에 대한 새 참조가 만들어지고 일반적으로 강력한 참조가됩니다. 다른 경우의 돌연변이를 일으키지 않는 지역 참고 자료이기도합니다. 습관에 의해 일반적으로 let
입니다. 그래서 그것은 평생 동안 같은 가치를 유지할 것입니다.
예를 들어 보겠습니다. 그러나 약간 조정하십시오.탐색 컨트롤러은 (its 내비게이션 적층 새 view 컨트롤러 추가 해당보기를 컨트롤러 오프 불쑥시 pushViewController)
으로 그것이 itself으로 동등한 view 컨트롤러의 navigationController
속성 전달으로 설정. 그리고 because as
if let navigationController = self.navigationController {
// this should always pass
assert(navigationController === self.navigationController)
navigationController.popViewControllerAnimated(false)
// this next assert may fail if `self` refers to the top VC on the nav stack
assert(navigationController === self.navigationController)
// we now add self back onto the nav stack
navigationController.pushViewController(self, animated: false)
// this should also always pass:
assert(navigationController === self.navigationController)
}
이
는 happening is 스택, 그것은
nil
다시 그 속성을 설정
의 다시 이러한 주장을 던지고, 옵션 - 체인 방식을 살펴 보자.
let navController = navigationController
// this should always pass
assert(navController === navigationController)
// assuming navigationController does not evaluate to nil here
navigationController?.popViewControllerAnimated(false)
// this may fail if `self` is top VC on nav stack
assert(navController === navigationController)
// if above failed, it's because navigation controller will now evaluate to nil
// if so, then the following optional chain will fail and nothing will happen
navigationController?.pushViewController(self, animated: false)
// if the previous assert fail, then the previous push wasn't called
// and if the previous push wasn't called, then navigation controller is still nil
// and therefore this assert would also fail:
assert(navController === navigationController)
을 그래서 여기, 우리의 채소를 self
의 nce 속성이 nil
으로 설정되어 있습니다.이 메서드 호출은 선택적인 바인딩 & 체인 간의 동작 차이를 일으 킵니다.
비록 우리가 현재 스레드에서 우리 자신의 행동으로 돌연변이를 일으키지 않았더라도 이전에 유효한 값을 가지고 있더라도 갑자기 속성이 nil
으로 평가되는 문제를 해결할 수 있습니다. 다중 스레드 코드를 사용하여 다른 스레드에서 값이 nil
으로 또는 그 반대로 평가되는 속성이 발생할 수 있습니다.
간단히 말해서 all-or-none을 확인해야하는 경우 선택적 바인딩을 선호해야합니다.
내비게이션 컨트롤러가없는 특정 컨트롤러에 상황이 있습니까? 이 속성은 선택 사항이지만 항법 컨트롤러 (Interface Builder에서)가 항상있는 경우 안전하게 포장 풀기 할 수 있습니다. – vadian