How to skip return value in function where value is expected












3














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?










share|improve this question




















  • 3




    use compactMap instead.
    – SeanLintern88
    Nov 13 '18 at 10:36
















3














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?










share|improve this question




















  • 3




    use compactMap instead.
    – SeanLintern88
    Nov 13 '18 at 10:36














3












3








3







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















2














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






share|improve this answer























  • 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



















0














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 of Double, 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)
}





share|improve this answer





















    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%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









    2














    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






    share|improve this answer























    • 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
















    2














    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






    share|improve this answer























    • 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














    2












    2








    2






    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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













    0














    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 of Double, 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)
    }





    share|improve this answer


























      0














      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 of Double, 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)
      }





      share|improve this answer
























        0












        0








        0






        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 of Double, 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)
        }





        share|improve this answer












        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 of Double, 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)
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 10:56









        vadianvadian

        144k13154170




        144k13154170






























            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%2f53279035%2fhow-to-skip-return-value-in-function-where-value-is-expected%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

            Bressuire

            Vorschmack

            Quarantine