swift - conflict with previous value in same-name Shadowing -
this question has answer here:
i'm reading erica sadun's swift style book , playing around code.
she suggests:
prefer using same-name shadowing in conditional bind. “this approach both conventional , safe:
guard let x = x { ... } // yes
guard let y = x { ... } // no
deliberate same-name shadowing ensures the unwrapped value’s role follows logical intent established name prior if or guard statement. introducing new name blurs line of semantics , can prove dangerous. consistent same-named shadowing prevents adding unintentional shadow symbols in parent scope.
regardless of being opinion or not:
in code, i'm trying do:
guard let data = data else{...}
but following error:
definition conflicts previous value
a simplified version of function is:
func process(){ data : anyobject? if x == nil{ data = m} else if x > 5{ data = j} guard let data = data else { return } // definition conflicts previous value somefunc(data) }
if place guard statement inside if
or else if
error go away. that's not want.
is there workaround still able same-name shadow.
edit: tried using backticks , following errors:
- expected pattern
- braced block of statements unused closure
Comments
Post a Comment