代码之家  ›  专栏  ›  技术社区  ›  Charlie Fish

作为单独参数的swift pass数组值

  •  0
  • Charlie Fish  · 技术社区  · 7 年前

    我使用以下代码 Bond .

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

    当然那是因为邦德 combineLatest 不接受选项。

    我考虑过做一个 if let 写两次基本相同的代码,但感觉很混乱。

    在javascript中,可以将每个元素作为单独参数的数组传递到函数中。如下所示:

    function test(a, b, c) {
        console.log("a", a);
        console.log("b", b);
        console.log("c", c);
    }
    
    const array = ["Hello", "world", "!!!"];
    test(...array);
    

    有没有办法迅速做到这一点?

    如果没有,有什么办法让我的代码工作,并尽可能干净?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Nilanshu Jaiswal    7 年前

    你可以传一个元组

    func test(aTuple: (String, String, String)) {
        print(aTuple.0)
        print(aTuple.1)
        print(aTuple.2)
    }
    
    let theTuple = ("Hello", "world", "!!!")
    test(aTuple: theTuple)
    

    希望有帮助。

        2
  •  0
  •   NRitH    7 年前

    如果所有参数都是同一类型,则可以使用 varargs 参数:

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

    你就这样称呼它

    test("Life", "Liberty", "Pursuit of Happiness")