Combining More Than Four @Published Variables in Combine?

If I want to subscribe to four @Published variables at the same time, I can do something like the following.

Publishers.CombineLatest4($variable0, $variable1, $variable2, $variable3)

I wonder if there is any solution to subscribing to more than four variables at the same time?

Muchos thankos

Answered by Tomato in 693616022

I guess it goes like the following.

Publishers.CombineLatest4($variable0, $variable1, $variable2, $variable3)
	.combineLatest($variable4)
	.combineLatest($variable5)
	.sink { completion in
		
	} receiveValue: { response0, response1 in
		let variable = response0.0
		let variable4 = response0.1
		let variable5 = response1
		let v0 = variable.0
		let v1 = variable.1
		let v2 = variable.2
		let v3 = variable.3
	}.store(in: &cancellables)

It's kind of odd.

Maybe, subscribing to an array of them?

Actually, no...

I thought

let publishers = Publishers.CombineLatest4($variable0, $variable1, $variable2, $variable3) .combineLatest($variable4) .combineLatest($variable5)

could work. But it doesn't.

Accepted Answer

I guess it goes like the following.

Publishers.CombineLatest4($variable0, $variable1, $variable2, $variable3)
	.combineLatest($variable4)
	.combineLatest($variable5)
	.sink { completion in
		
	} receiveValue: { response0, response1 in
		let variable = response0.0
		let variable4 = response0.1
		let variable5 = response1
		let v0 = variable.0
		let v1 = variable.1
		let v2 = variable.2
		let v3 = variable.3
	}.store(in: &cancellables)

It's kind of odd.

You can write it a little more compact:

        $variable0
            .combineLatest($variable1, $variable2, $variable3)
            .combineLatest($variable4, $variable5)
            .sink { completion in
                
            } receiveValue: {
                (tuple, v4 ,v5) in
                let (v0, v1, v2, v3) = tuple
            }.store(in: &cancellables)
Combining More Than Four @Published Variables in Combine?
 
 
Q