Swift Pass Array Values as Separate Parameters
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
add a comment |
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
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
add a comment |
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
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
ios swift swiftbond
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
That isn't a true array tho. So at no point could I filter out thenil
/optional results. Which is what I need to do to make that code more clean.
– Charlie Fish
Nov 20 '18 at 2:24
add a comment |
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")
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
That isn't a true array tho. So at no point could I filter out thenil
/optional results. Which is what I need to do to make that code more clean.
– Charlie Fish
Nov 20 '18 at 2:24
add a comment |
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.
That isn't a true array tho. So at no point could I filter out thenil
/optional results. Which is what I need to do to make that code more clean.
– Charlie Fish
Nov 20 '18 at 2:24
add a comment |
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.
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.
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 thenil
/optional results. Which is what I need to do to make that code more clean.
– Charlie Fish
Nov 20 '18 at 2:24
add a comment |
That isn't a true array tho. So at no point could I filter out thenil
/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
add a comment |
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")
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
add a comment |
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")
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
add a comment |
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")
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")
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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