0

I have the following code using Bond.

combineLatest(settings.userAutoRefreshInterval, config.value?.userRefreshInterval).observeNext { [weak self] _ in
    self?.updateAutoUserRefresh()
}.dispose(in: self.bag)

Of course, that has a build error since Bond's combineLatest doesn't accept optionals.

I have considered doing an if let and writing basically the same code twice, but that feels really messy.

In JavaScript, you can pass in an array with each element as a separate parameter into a function. Like the following:

function test(a, b, c) {
    console.log("a", a);
    console.log("b", b);
    console.log("c", c);
}

const array = ["Hello", "world", "!!!"];
test(...array);

Is there a way to do that in Swift?

If not, any ideas for how to get my code working and have it be as clean as possible?

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179

2 Answers2

0

You can pass a tuple

func test(aTuple: (String, String, String)) {
    print(aTuple.0)
    print(aTuple.1)
    print(aTuple.2)
}

let theTuple = ("Hello", "world", "!!!")
test(aTuple: theTuple)

Hope it helps.

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
  • That isn't a true array tho. So at no point could I filter out the `nil`/optional results. Which is what I need to do to make that code more clean. – Charlie Fish Nov 20 '18 at 02:24
0

If all of your arguments are the same type, then you can use varargs parameters:

func test(_ args: String...) {
    args.forEach { print $0 }
}

You then call it like

test("Life", "Liberty", "Pursuit of Happiness")
NRitH
  • 13,441
  • 4
  • 41
  • 44
  • Ok, that looks promising, but I just realized that looks like it would require changes to Bond, and not something I could do. – Charlie Fish Nov 20 '18 at 03:18