Says type is non-optional but prints optional











up vote
-2
down vote

favorite












I have this code:



let result_string = "(String(describing: value))"


because I need to convert this from type Any to String. However when I print result string, it prints:



optional(abcd)


How do I make it print just



abcd


?



When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.



EDIT:
Value was an optional, so force unwrapping it fixed my problem.



let result_string = "(String(describing: value!))"









share|improve this question









New contributor




Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • My guess: value is an optional ...
    – Martin R
    yesterday










  • @Martin R So should I try force unwrapping value?
    – Athreya Daniel
    yesterday










  • @MartinR Thank You, force unwrapping this fixed my problem.
    – Athreya Daniel
    yesterday






  • 2




    force unwrap may crash if the value is nil
    – Sh_Khan
    yesterday












  • @AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
    – Prabhat
    10 hours ago















up vote
-2
down vote

favorite












I have this code:



let result_string = "(String(describing: value))"


because I need to convert this from type Any to String. However when I print result string, it prints:



optional(abcd)


How do I make it print just



abcd


?



When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.



EDIT:
Value was an optional, so force unwrapping it fixed my problem.



let result_string = "(String(describing: value!))"









share|improve this question









New contributor




Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • My guess: value is an optional ...
    – Martin R
    yesterday










  • @Martin R So should I try force unwrapping value?
    – Athreya Daniel
    yesterday










  • @MartinR Thank You, force unwrapping this fixed my problem.
    – Athreya Daniel
    yesterday






  • 2




    force unwrap may crash if the value is nil
    – Sh_Khan
    yesterday












  • @AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
    – Prabhat
    10 hours ago













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have this code:



let result_string = "(String(describing: value))"


because I need to convert this from type Any to String. However when I print result string, it prints:



optional(abcd)


How do I make it print just



abcd


?



When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.



EDIT:
Value was an optional, so force unwrapping it fixed my problem.



let result_string = "(String(describing: value!))"









share|improve this question









New contributor




Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have this code:



let result_string = "(String(describing: value))"


because I need to convert this from type Any to String. However when I print result string, it prints:



optional(abcd)


How do I make it print just



abcd


?



When I tried the other fixes for optionals it told me that result_string is not optional and didn't let me force unwrap.



EDIT:
Value was an optional, so force unwrapping it fixed my problem.



let result_string = "(String(describing: value!))"






swift xcode






share|improve this question









New contributor




Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday





















New contributor




Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Athreya Daniel

11




11




New contributor




Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Athreya Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • My guess: value is an optional ...
    – Martin R
    yesterday










  • @Martin R So should I try force unwrapping value?
    – Athreya Daniel
    yesterday










  • @MartinR Thank You, force unwrapping this fixed my problem.
    – Athreya Daniel
    yesterday






  • 2




    force unwrap may crash if the value is nil
    – Sh_Khan
    yesterday












  • @AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
    – Prabhat
    10 hours ago


















  • My guess: value is an optional ...
    – Martin R
    yesterday










  • @Martin R So should I try force unwrapping value?
    – Athreya Daniel
    yesterday










  • @MartinR Thank You, force unwrapping this fixed my problem.
    – Athreya Daniel
    yesterday






  • 2




    force unwrap may crash if the value is nil
    – Sh_Khan
    yesterday












  • @AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
    – Prabhat
    10 hours ago
















My guess: value is an optional ...
– Martin R
yesterday




My guess: value is an optional ...
– Martin R
yesterday












@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday




@Martin R So should I try force unwrapping value?
– Athreya Daniel
yesterday












@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday




@MartinR Thank You, force unwrapping this fixed my problem.
– Athreya Daniel
yesterday




2




2




force unwrap may crash if the value is nil
– Sh_Khan
yesterday






force unwrap may crash if the value is nil
– Sh_Khan
yesterday














@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago




@AthreyaDaniel your result_string is optional but value which is type Any is not optional and you are trying to force unwrap non-optional value.
– Prabhat
10 hours ago












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You can try



if let res = value {
print(res)
}


OR



guard let res = value { return }
print(res)





share|improve this answer




























    up vote
    -1
    down vote













    you can try like below:



    var value : Any? = "abcd"



    let result_string = value as! String



    print(result_string)



    We have to downcast Any to string.
    Worked in playground.






    share|improve this answer























    • Sorry Prabhat but this is wrong. In your example, result_string is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
      – Moritz
      9 hours ago










    • @Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
      – Prabhat
      8 hours ago










    • Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example result_string is not an optional. Hint: "interpolation"... ;)
      – Moritz
      8 hours ago










    • Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
      – Prabhat
      8 hours ago













    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',
    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
    });


    }
    });






    Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401637%2fsays-type-is-non-optional-but-prints-optional%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








    up vote
    0
    down vote













    You can try



    if let res = value {
    print(res)
    }


    OR



    guard let res = value { return }
    print(res)





    share|improve this answer

























      up vote
      0
      down vote













      You can try



      if let res = value {
      print(res)
      }


      OR



      guard let res = value { return }
      print(res)





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can try



        if let res = value {
        print(res)
        }


        OR



        guard let res = value { return }
        print(res)





        share|improve this answer












        You can try



        if let res = value {
        print(res)
        }


        OR



        guard let res = value { return }
        print(res)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Sh_Khan

        34.2k41125




        34.2k41125
























            up vote
            -1
            down vote













            you can try like below:



            var value : Any? = "abcd"



            let result_string = value as! String



            print(result_string)



            We have to downcast Any to string.
            Worked in playground.






            share|improve this answer























            • Sorry Prabhat but this is wrong. In your example, result_string is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
              – Moritz
              9 hours ago










            • @Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
              – Prabhat
              8 hours ago










            • Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example result_string is not an optional. Hint: "interpolation"... ;)
              – Moritz
              8 hours ago










            • Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
              – Prabhat
              8 hours ago

















            up vote
            -1
            down vote













            you can try like below:



            var value : Any? = "abcd"



            let result_string = value as! String



            print(result_string)



            We have to downcast Any to string.
            Worked in playground.






            share|improve this answer























            • Sorry Prabhat but this is wrong. In your example, result_string is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
              – Moritz
              9 hours ago










            • @Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
              – Prabhat
              8 hours ago










            • Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example result_string is not an optional. Hint: "interpolation"... ;)
              – Moritz
              8 hours ago










            • Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
              – Prabhat
              8 hours ago















            up vote
            -1
            down vote










            up vote
            -1
            down vote









            you can try like below:



            var value : Any? = "abcd"



            let result_string = value as! String



            print(result_string)



            We have to downcast Any to string.
            Worked in playground.






            share|improve this answer














            you can try like below:



            var value : Any? = "abcd"



            let result_string = value as! String



            print(result_string)



            We have to downcast Any to string.
            Worked in playground.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 8 hours ago

























            answered 10 hours ago









            Prabhat

            493




            493












            • Sorry Prabhat but this is wrong. In your example, result_string is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
              – Moritz
              9 hours ago










            • @Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
              – Prabhat
              8 hours ago










            • Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example result_string is not an optional. Hint: "interpolation"... ;)
              – Moritz
              8 hours ago










            • Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
              – Prabhat
              8 hours ago




















            • Sorry Prabhat but this is wrong. In your example, result_string is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
              – Moritz
              9 hours ago










            • @Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
              – Prabhat
              8 hours ago










            • Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example result_string is not an optional. Hint: "interpolation"... ;)
              – Moritz
              8 hours ago










            • Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
              – Prabhat
              8 hours ago


















            Sorry Prabhat but this is wrong. In your example, result_string is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
            – Moritz
            9 hours ago




            Sorry Prabhat but this is wrong. In your example, result_string is a non-optional string containing the literal string "Optional(value)". Same mistake as OP's.
            – Moritz
            9 hours ago












            @Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
            – Prabhat
            8 hours ago




            @Moritz If value is type Any with optional then it will produce Optional(value) and I was consi
            – Prabhat
            8 hours ago












            Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example result_string is not an optional. Hint: "interpolation"... ;)
            – Moritz
            8 hours ago




            Please test your code in a Playground, Prabhat. You will see that you get "initializer for conditional binding must have Optional type, not 'String'", because as I said, in your example result_string is not an optional. Hint: "interpolation"... ;)
            – Moritz
            8 hours ago












            Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
            – Prabhat
            8 hours ago






            Thanks got it. We have to downcast value of type Any . I have edited the answer and verified it.
            – Prabhat
            8 hours ago












            Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.













            Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.












            Athreya Daniel is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401637%2fsays-type-is-non-optional-but-prints-optional%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

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...