I am confused about the definition of a block of code or "area". Apple security docs talk about it: else block security instructions ...
"must transfer control to exit the code block in which the security instruction appears."
Other online sources say that the βscopeβ in which it exists must be present in the security instruction.
So take the sample code below:
func testGuardControlFlow () { let x = 2 let y = 2 func embededFunc () { if y == 2 { guard x == 1 else { print("oops, number is not 1") return } print ("from in embededFunc") } print ("I still want this to print even if x != 1") } embededFunc() print("Great, return still allows this to be printed.") } testGuardControlFlow()
According to my current understanding of "scope", the code
if y == 2 {....}
creates a new scope, namely between {}. And, given this assumption, the guard would simply have avoided this area. But this is not the case. Guard in this case leaves the function in which it is placed, regardless of whether it is similar in the if clause.
Am I completely misunderstanding what "scope" means? Does the scope include the code contained in the method? If so, what is the correct term for "space" that exists in an if statement?
scope swift
thecloud_of_unknowing
source share