How to skip return value in function where value is expected
This is the function I am using:
let values = (0..<self.data.count).map { (i) -> ChartDataEntry in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
} else {
return nil
}
}
This is causing this error:
Nil is incompatible with return type 'ChartDataEntry'
How can I skip the loop without returning any value and continue with next check?
swift return
add a comment |
This is the function I am using:
let values = (0..<self.data.count).map { (i) -> ChartDataEntry in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
} else {
return nil
}
}
This is causing this error:
Nil is incompatible with return type 'ChartDataEntry'
How can I skip the loop without returning any value and continue with next check?
swift return
3
use compactMap instead.
– SeanLintern88
Nov 13 '18 at 10:36
add a comment |
This is the function I am using:
let values = (0..<self.data.count).map { (i) -> ChartDataEntry in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
} else {
return nil
}
}
This is causing this error:
Nil is incompatible with return type 'ChartDataEntry'
How can I skip the loop without returning any value and continue with next check?
swift return
This is the function I am using:
let values = (0..<self.data.count).map { (i) -> ChartDataEntry in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
} else {
return nil
}
}
This is causing this error:
Nil is incompatible with return type 'ChartDataEntry'
How can I skip the loop without returning any value and continue with next check?
swift return
swift return
edited Nov 13 '18 at 10:36
Dávid Pásztor
20.7k72547
20.7k72547
asked Nov 13 '18 at 10:33
sddsdd
429625
429625
3
use compactMap instead.
– SeanLintern88
Nov 13 '18 at 10:36
add a comment |
3
use compactMap instead.
– SeanLintern88
Nov 13 '18 at 10:36
3
3
use compactMap instead.
– SeanLintern88
Nov 13 '18 at 10:36
use compactMap instead.
– SeanLintern88
Nov 13 '18 at 10:36
add a comment |
2 Answers
2
active
oldest
votes
let values = (0..<self.data.count).compactMap { (i) -> ChartDataEntry? in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
}
return nil
}
Replace your code with this.
compactMap
- Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
You can read more about compactMap
here
Thank you it worked!
– sdd
Nov 13 '18 at 10:44
@sdd can u pls mark the answer if the answer is helpful to you? thanks.
– Aakash
Nov 13 '18 at 10:51
add a comment |
Based on the correct answer of Aakash to use compactMap
this is a swiftier (and more efficient) version
x
is a constant, so create it once outside of the closure.
TimeInterval
is a type alias ofDouble
,Double(x)
is redundant.- (Compact)map the
data
array, not its indices. - Avoid unnecessary bridge casts.
let x = valueDate!.timeIntervalSince(startDate)
let values = self.data.compactMap { (item) -> ChartDataEntry? in
guard let d = item["labelvalue"] as? String, let a = Double(d) else { return nil }
return ChartDataEntry(x: x, y: a)
}
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%2f53279035%2fhow-to-skip-return-value-in-function-where-value-is-expected%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
let values = (0..<self.data.count).compactMap { (i) -> ChartDataEntry? in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
}
return nil
}
Replace your code with this.
compactMap
- Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
You can read more about compactMap
here
Thank you it worked!
– sdd
Nov 13 '18 at 10:44
@sdd can u pls mark the answer if the answer is helpful to you? thanks.
– Aakash
Nov 13 '18 at 10:51
add a comment |
let values = (0..<self.data.count).compactMap { (i) -> ChartDataEntry? in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
}
return nil
}
Replace your code with this.
compactMap
- Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
You can read more about compactMap
here
Thank you it worked!
– sdd
Nov 13 '18 at 10:44
@sdd can u pls mark the answer if the answer is helpful to you? thanks.
– Aakash
Nov 13 '18 at 10:51
add a comment |
let values = (0..<self.data.count).compactMap { (i) -> ChartDataEntry? in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
}
return nil
}
Replace your code with this.
compactMap
- Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
You can read more about compactMap
here
let values = (0..<self.data.count).compactMap { (i) -> ChartDataEntry? in
let x = valueDate!.timeIntervalSince(startDate)
if let a = (self.data[i]["labelvalue"] as? NSString)?.doubleValue{
return ChartDataEntry(x: Double(x), y: a)
}
return nil
}
Replace your code with this.
compactMap
- Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
You can read more about compactMap
here
edited Nov 13 '18 at 10:50
Ahmad F
16.1k104080
16.1k104080
answered Nov 13 '18 at 10:36
AakashAakash
1,343715
1,343715
Thank you it worked!
– sdd
Nov 13 '18 at 10:44
@sdd can u pls mark the answer if the answer is helpful to you? thanks.
– Aakash
Nov 13 '18 at 10:51
add a comment |
Thank you it worked!
– sdd
Nov 13 '18 at 10:44
@sdd can u pls mark the answer if the answer is helpful to you? thanks.
– Aakash
Nov 13 '18 at 10:51
Thank you it worked!
– sdd
Nov 13 '18 at 10:44
Thank you it worked!
– sdd
Nov 13 '18 at 10:44
@sdd can u pls mark the answer if the answer is helpful to you? thanks.
– Aakash
Nov 13 '18 at 10:51
@sdd can u pls mark the answer if the answer is helpful to you? thanks.
– Aakash
Nov 13 '18 at 10:51
add a comment |
Based on the correct answer of Aakash to use compactMap
this is a swiftier (and more efficient) version
x
is a constant, so create it once outside of the closure.
TimeInterval
is a type alias ofDouble
,Double(x)
is redundant.- (Compact)map the
data
array, not its indices. - Avoid unnecessary bridge casts.
let x = valueDate!.timeIntervalSince(startDate)
let values = self.data.compactMap { (item) -> ChartDataEntry? in
guard let d = item["labelvalue"] as? String, let a = Double(d) else { return nil }
return ChartDataEntry(x: x, y: a)
}
add a comment |
Based on the correct answer of Aakash to use compactMap
this is a swiftier (and more efficient) version
x
is a constant, so create it once outside of the closure.
TimeInterval
is a type alias ofDouble
,Double(x)
is redundant.- (Compact)map the
data
array, not its indices. - Avoid unnecessary bridge casts.
let x = valueDate!.timeIntervalSince(startDate)
let values = self.data.compactMap { (item) -> ChartDataEntry? in
guard let d = item["labelvalue"] as? String, let a = Double(d) else { return nil }
return ChartDataEntry(x: x, y: a)
}
add a comment |
Based on the correct answer of Aakash to use compactMap
this is a swiftier (and more efficient) version
x
is a constant, so create it once outside of the closure.
TimeInterval
is a type alias ofDouble
,Double(x)
is redundant.- (Compact)map the
data
array, not its indices. - Avoid unnecessary bridge casts.
let x = valueDate!.timeIntervalSince(startDate)
let values = self.data.compactMap { (item) -> ChartDataEntry? in
guard let d = item["labelvalue"] as? String, let a = Double(d) else { return nil }
return ChartDataEntry(x: x, y: a)
}
Based on the correct answer of Aakash to use compactMap
this is a swiftier (and more efficient) version
x
is a constant, so create it once outside of the closure.
TimeInterval
is a type alias ofDouble
,Double(x)
is redundant.- (Compact)map the
data
array, not its indices. - Avoid unnecessary bridge casts.
let x = valueDate!.timeIntervalSince(startDate)
let values = self.data.compactMap { (item) -> ChartDataEntry? in
guard let d = item["labelvalue"] as? String, let a = Double(d) else { return nil }
return ChartDataEntry(x: x, y: a)
}
answered Nov 13 '18 at 10:56
vadianvadian
144k13154170
144k13154170
add a comment |
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%2f53279035%2fhow-to-skip-return-value-in-function-where-value-is-expected%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
3
use compactMap instead.
– SeanLintern88
Nov 13 '18 at 10:36