Swift Pass Array Values as Separate Parameters












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 seperate 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?










share|improve this question























  • Why not pass only one parameter (the array) and use its values inside the function?

    – Damon
    Nov 20 '18 at 2:33






  • 1





    Not supported yet. This ticket has been open for 3 years.

    – Code Different
    Nov 20 '18 at 3:05













  • @Damon That function is part of Bond. I didn't create it.

    – Charlie Fish
    Nov 20 '18 at 3:19
















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 seperate 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?










share|improve this question























  • Why not pass only one parameter (the array) and use its values inside the function?

    – Damon
    Nov 20 '18 at 2:33






  • 1





    Not supported yet. This ticket has been open for 3 years.

    – Code Different
    Nov 20 '18 at 3:05













  • @Damon That function is part of Bond. I didn't create it.

    – Charlie Fish
    Nov 20 '18 at 3:19














0












0








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 seperate 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?










share|improve this question














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 seperate 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?







ios swift swiftbond






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 2:15









Charlie FishCharlie Fish

5,48942972




5,48942972













  • Why not pass only one parameter (the array) and use its values inside the function?

    – Damon
    Nov 20 '18 at 2:33






  • 1





    Not supported yet. This ticket has been open for 3 years.

    – Code Different
    Nov 20 '18 at 3:05













  • @Damon That function is part of Bond. I didn't create it.

    – Charlie Fish
    Nov 20 '18 at 3:19



















  • Why not pass only one parameter (the array) and use its values inside the function?

    – Damon
    Nov 20 '18 at 2:33






  • 1





    Not supported yet. This ticket has been open for 3 years.

    – Code Different
    Nov 20 '18 at 3:05













  • @Damon That function is part of Bond. I didn't create it.

    – Charlie Fish
    Nov 20 '18 at 3:19

















Why not pass only one parameter (the array) and use its values inside the function?

– Damon
Nov 20 '18 at 2:33





Why not pass only one parameter (the array) and use its values inside the function?

– Damon
Nov 20 '18 at 2:33




1




1





Not supported yet. This ticket has been open for 3 years.

– Code Different
Nov 20 '18 at 3:05







Not supported yet. This ticket has been open for 3 years.

– Code Different
Nov 20 '18 at 3:05















@Damon That function is part of Bond. I didn't create it.

– Charlie Fish
Nov 20 '18 at 3:19





@Damon That function is part of Bond. I didn't create it.

– Charlie Fish
Nov 20 '18 at 3:19












2 Answers
2






active

oldest

votes


















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.






share|improve this answer
























  • 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 2: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")





share|improve this answer
























  • 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 3:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385274%2fswift-pass-array-values-as-separate-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer
























  • 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 2:24
















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.






share|improve this answer
























  • 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 2:24














0












0








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 2:22









DamonDamon

524318




524318













  • 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 2:24



















  • 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 2:24

















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 2:24





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 2: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")





share|improve this answer
























  • 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 3:18
















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")





share|improve this answer
























  • 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 3:18














0












0








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")





share|improve this answer













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")






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 2:59









NRitHNRitH

7,61212532




7,61212532













  • 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 3:18



















  • 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 3:18

















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 3:18





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 3:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385274%2fswift-pass-array-values-as-separate-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]