Tuple with `let` or` var`

Let's say I have something like this:

func myFunc() -> (Any, Any)? {...}

func anotherFunc() {
    if var (a, b) = myFunc() {
        // a is mutated and accessed; b is accessed, but not mutated
        a = b
    }
}

I need it varfor the tuple, because it is amutated, but Xcode complains that " bnever mutated, think about using let" - this is a somewhat reasonable argument, but I can not define the tuple as (var, let).

I suggest that I could use an index to access two elements instead of declaring them, bypassing this problem. But is there a better way?

+4
source share
1 answer

(.. if case), , :

if case (var a, let b)? = myFunc() {
    a = b
}

x?, myFunc(), , .

+8

Source: https://habr.com/ru/post/1677381/


All Articles