Non optional String automatically getting changed to optionalHow to change the name of an iOS app?How can I make a UITextField move up when the keyboard is present - on starting to edit?How do I check if a string contains another string in Objective-C?Where are iOS simulator screenshots stored?Convert between UIImage and Base64 stringUIScrollView Scrollable Content Size AmbiguityUsing a dispatch_once singleton model in SwiftWhat is an “unwrapped value” in Swift?Get the length of a StringiOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta

I see my dog run

Is it wise to hold on to stock that has plummeted and then stabilized?

Why is my log file so massive? 22gb. I am running log backups

extract characters between two commas?

What do the Banks children have against barley water?

Are cabin dividers used to "hide" the flex of the airplane?

"listening to me about as much as you're listening to this pole here"

Is it wise to focus on putting odd beats on left when playing double bass drums?

Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?

What causes the sudden spool-up sound from an F-16 when enabling afterburner?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Why doesn't a const reference extend the life of a temporary object passed via a function?

Why was the "bread communication" in the arena of Catching Fire left out in the movie?

Prime joint compound before latex paint?

Landing in very high winds

How can I plot a Farey diagram?

What is the command to reset a PC without deleting any files

aging parents with no investments

What is it called when one voice type sings a 'solo'?

How to deal with fear of taking dependencies

New order #4: World

Doomsday-clock for my fantasy planet

How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)

Why do we use polarized capacitors?



Non optional String automatically getting changed to optional


How to change the name of an iOS app?How can I make a UITextField move up when the keyboard is present - on starting to edit?How do I check if a string contains another string in Objective-C?Where are iOS simulator screenshots stored?Convert between UIImage and Base64 stringUIScrollView Scrollable Content Size AmbiguityUsing a dispatch_once singleton model in SwiftWhat is an “unwrapped value” in Swift?Get the length of a StringiOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-4















I am facing a problem where the object which I have created is a non-optional value but somehow it is getting converted into an optional one.



I have tried in the playground also refer to the screenshot below. Does anyone know what am I missing?



enter image description here










share|improve this question






















  • someProperty is optional. What are you talking about? String! is an implicitly unwrapped optional.

    – Sweeper
    Mar 8 at 6:51











  • did you have any specific reason for making someProperty a var instead of initializing it as a let constant?

    – NSCoder
    Mar 8 at 6:58







  • 1





    Don't post code as images, post it as text.

    – Joakim Danielson
    Mar 8 at 6:58






  • 1





    Never ever declare a property as implicit unwrapped optional which is initialized in an init method passing a non-optional value. Never. Remove the exclamation mark. If you really need an optional declare it as regular optional (?) and make the parameter optional, too.

    – vadian
    Mar 8 at 7:29


















-4















I am facing a problem where the object which I have created is a non-optional value but somehow it is getting converted into an optional one.



I have tried in the playground also refer to the screenshot below. Does anyone know what am I missing?



enter image description here










share|improve this question






















  • someProperty is optional. What are you talking about? String! is an implicitly unwrapped optional.

    – Sweeper
    Mar 8 at 6:51











  • did you have any specific reason for making someProperty a var instead of initializing it as a let constant?

    – NSCoder
    Mar 8 at 6:58







  • 1





    Don't post code as images, post it as text.

    – Joakim Danielson
    Mar 8 at 6:58






  • 1





    Never ever declare a property as implicit unwrapped optional which is initialized in an init method passing a non-optional value. Never. Remove the exclamation mark. If you really need an optional declare it as regular optional (?) and make the parameter optional, too.

    – vadian
    Mar 8 at 7:29














-4












-4








-4








I am facing a problem where the object which I have created is a non-optional value but somehow it is getting converted into an optional one.



I have tried in the playground also refer to the screenshot below. Does anyone know what am I missing?



enter image description here










share|improve this question














I am facing a problem where the object which I have created is a non-optional value but somehow it is getting converted into an optional one.



I have tried in the playground also refer to the screenshot below. Does anyone know what am I missing?



enter image description here







ios swift swift-playground






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 6:48









Khushal DugarKhushal Dugar

158211




158211












  • someProperty is optional. What are you talking about? String! is an implicitly unwrapped optional.

    – Sweeper
    Mar 8 at 6:51











  • did you have any specific reason for making someProperty a var instead of initializing it as a let constant?

    – NSCoder
    Mar 8 at 6:58







  • 1





    Don't post code as images, post it as text.

    – Joakim Danielson
    Mar 8 at 6:58






  • 1





    Never ever declare a property as implicit unwrapped optional which is initialized in an init method passing a non-optional value. Never. Remove the exclamation mark. If you really need an optional declare it as regular optional (?) and make the parameter optional, too.

    – vadian
    Mar 8 at 7:29


















  • someProperty is optional. What are you talking about? String! is an implicitly unwrapped optional.

    – Sweeper
    Mar 8 at 6:51











  • did you have any specific reason for making someProperty a var instead of initializing it as a let constant?

    – NSCoder
    Mar 8 at 6:58







  • 1





    Don't post code as images, post it as text.

    – Joakim Danielson
    Mar 8 at 6:58






  • 1





    Never ever declare a property as implicit unwrapped optional which is initialized in an init method passing a non-optional value. Never. Remove the exclamation mark. If you really need an optional declare it as regular optional (?) and make the parameter optional, too.

    – vadian
    Mar 8 at 7:29

















someProperty is optional. What are you talking about? String! is an implicitly unwrapped optional.

– Sweeper
Mar 8 at 6:51





someProperty is optional. What are you talking about? String! is an implicitly unwrapped optional.

– Sweeper
Mar 8 at 6:51













did you have any specific reason for making someProperty a var instead of initializing it as a let constant?

– NSCoder
Mar 8 at 6:58






did you have any specific reason for making someProperty a var instead of initializing it as a let constant?

– NSCoder
Mar 8 at 6:58





1




1





Don't post code as images, post it as text.

– Joakim Danielson
Mar 8 at 6:58





Don't post code as images, post it as text.

– Joakim Danielson
Mar 8 at 6:58




1




1





Never ever declare a property as implicit unwrapped optional which is initialized in an init method passing a non-optional value. Never. Remove the exclamation mark. If you really need an optional declare it as regular optional (?) and make the parameter optional, too.

– vadian
Mar 8 at 7:29






Never ever declare a property as implicit unwrapped optional which is initialized in an init method passing a non-optional value. Never. Remove the exclamation mark. If you really need an optional declare it as regular optional (?) and make the parameter optional, too.

– vadian
Mar 8 at 7:29













3 Answers
3






active

oldest

votes


















0














Simply remove the implicitly unwrapped optional sign !. Do the class like this:



import UIKit

class SomeDataModelClass
var someProperty: String

init(someProperty: String)
self.someProperty = someProperty



let someObject = SomeDataModelClass(someProperty: "Non Optional String")
print(someObject.someProperty)


And you won't get the previous result.






share|improve this answer






























    0














    Implicitly unwrapped optionals (any type suffixed with a !) are optionals, underneath the surface. This recently changed in Swift 4+. Here is an excerpt from the Swift.org blog Reimplementation of Implicitly Unwrapped Optionals (with emphasis added):




    Implicitly unwrapped optionals are optionals that are automatically unwrapped if needed for an expression to compile. To declare an optional that’s implicitly unwrapped, place a ! after the type name rather than a ?.



    A mental model many people have for implicitly unwrapped optionals is that they are a type, distinct from regular optionals. In Swift 3, that was exactly how they worked: declarations like var a: Int? would result in a having type Optional<Int>, and declarations like var b: String! would result in b having type ImplicitlyUnwrappedOptional<String>.



    The new mental model for IUOs is one where you consider ! to be a synonym for ? with the addition that it adds a flag on the declaration letting the compiler know that the declared value can be implicitly unwrapped.




    In your case, specifically, you are printing an implicitly unwrapped optional, which is really just a fancy optional, so it’s expected & correct behavior. If you want to print the string value itself, you’ll need to explicitly unwrap the value, print(someObject.someProperty!). The linked blog post deep dives into the new reimplementation of IUOs & is worth a skim.






    share|improve this answer






























      0














      Implicitly unwrapped optional is still optional. It just doesn’t look like that when you’re writing your code. So this property still can hold no value.




      But for your specific need there is no reason to having implicitly unwrapped optional. Make it either non-optional or optional. Also you can use struct instead of class and then you can use default memberwise intializer



      struct Model 
      var property: String


      let object = Model(property: "text")
      print(object.property)



      text







      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%2f55058096%2fnon-optional-string-automatically-getting-changed-to-optional%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Simply remove the implicitly unwrapped optional sign !. Do the class like this:



        import UIKit

        class SomeDataModelClass
        var someProperty: String

        init(someProperty: String)
        self.someProperty = someProperty



        let someObject = SomeDataModelClass(someProperty: "Non Optional String")
        print(someObject.someProperty)


        And you won't get the previous result.






        share|improve this answer



























          0














          Simply remove the implicitly unwrapped optional sign !. Do the class like this:



          import UIKit

          class SomeDataModelClass
          var someProperty: String

          init(someProperty: String)
          self.someProperty = someProperty



          let someObject = SomeDataModelClass(someProperty: "Non Optional String")
          print(someObject.someProperty)


          And you won't get the previous result.






          share|improve this answer

























            0












            0








            0







            Simply remove the implicitly unwrapped optional sign !. Do the class like this:



            import UIKit

            class SomeDataModelClass
            var someProperty: String

            init(someProperty: String)
            self.someProperty = someProperty



            let someObject = SomeDataModelClass(someProperty: "Non Optional String")
            print(someObject.someProperty)


            And you won't get the previous result.






            share|improve this answer













            Simply remove the implicitly unwrapped optional sign !. Do the class like this:



            import UIKit

            class SomeDataModelClass
            var someProperty: String

            init(someProperty: String)
            self.someProperty = someProperty



            let someObject = SomeDataModelClass(someProperty: "Non Optional String")
            print(someObject.someProperty)


            And you won't get the previous result.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 8 at 6:57









            NSCoderNSCoder

            13813




            13813























                0














                Implicitly unwrapped optionals (any type suffixed with a !) are optionals, underneath the surface. This recently changed in Swift 4+. Here is an excerpt from the Swift.org blog Reimplementation of Implicitly Unwrapped Optionals (with emphasis added):




                Implicitly unwrapped optionals are optionals that are automatically unwrapped if needed for an expression to compile. To declare an optional that’s implicitly unwrapped, place a ! after the type name rather than a ?.



                A mental model many people have for implicitly unwrapped optionals is that they are a type, distinct from regular optionals. In Swift 3, that was exactly how they worked: declarations like var a: Int? would result in a having type Optional<Int>, and declarations like var b: String! would result in b having type ImplicitlyUnwrappedOptional<String>.



                The new mental model for IUOs is one where you consider ! to be a synonym for ? with the addition that it adds a flag on the declaration letting the compiler know that the declared value can be implicitly unwrapped.




                In your case, specifically, you are printing an implicitly unwrapped optional, which is really just a fancy optional, so it’s expected & correct behavior. If you want to print the string value itself, you’ll need to explicitly unwrap the value, print(someObject.someProperty!). The linked blog post deep dives into the new reimplementation of IUOs & is worth a skim.






                share|improve this answer



























                  0














                  Implicitly unwrapped optionals (any type suffixed with a !) are optionals, underneath the surface. This recently changed in Swift 4+. Here is an excerpt from the Swift.org blog Reimplementation of Implicitly Unwrapped Optionals (with emphasis added):




                  Implicitly unwrapped optionals are optionals that are automatically unwrapped if needed for an expression to compile. To declare an optional that’s implicitly unwrapped, place a ! after the type name rather than a ?.



                  A mental model many people have for implicitly unwrapped optionals is that they are a type, distinct from regular optionals. In Swift 3, that was exactly how they worked: declarations like var a: Int? would result in a having type Optional<Int>, and declarations like var b: String! would result in b having type ImplicitlyUnwrappedOptional<String>.



                  The new mental model for IUOs is one where you consider ! to be a synonym for ? with the addition that it adds a flag on the declaration letting the compiler know that the declared value can be implicitly unwrapped.




                  In your case, specifically, you are printing an implicitly unwrapped optional, which is really just a fancy optional, so it’s expected & correct behavior. If you want to print the string value itself, you’ll need to explicitly unwrap the value, print(someObject.someProperty!). The linked blog post deep dives into the new reimplementation of IUOs & is worth a skim.






                  share|improve this answer

























                    0












                    0








                    0







                    Implicitly unwrapped optionals (any type suffixed with a !) are optionals, underneath the surface. This recently changed in Swift 4+. Here is an excerpt from the Swift.org blog Reimplementation of Implicitly Unwrapped Optionals (with emphasis added):




                    Implicitly unwrapped optionals are optionals that are automatically unwrapped if needed for an expression to compile. To declare an optional that’s implicitly unwrapped, place a ! after the type name rather than a ?.



                    A mental model many people have for implicitly unwrapped optionals is that they are a type, distinct from regular optionals. In Swift 3, that was exactly how they worked: declarations like var a: Int? would result in a having type Optional<Int>, and declarations like var b: String! would result in b having type ImplicitlyUnwrappedOptional<String>.



                    The new mental model for IUOs is one where you consider ! to be a synonym for ? with the addition that it adds a flag on the declaration letting the compiler know that the declared value can be implicitly unwrapped.




                    In your case, specifically, you are printing an implicitly unwrapped optional, which is really just a fancy optional, so it’s expected & correct behavior. If you want to print the string value itself, you’ll need to explicitly unwrap the value, print(someObject.someProperty!). The linked blog post deep dives into the new reimplementation of IUOs & is worth a skim.






                    share|improve this answer













                    Implicitly unwrapped optionals (any type suffixed with a !) are optionals, underneath the surface. This recently changed in Swift 4+. Here is an excerpt from the Swift.org blog Reimplementation of Implicitly Unwrapped Optionals (with emphasis added):




                    Implicitly unwrapped optionals are optionals that are automatically unwrapped if needed for an expression to compile. To declare an optional that’s implicitly unwrapped, place a ! after the type name rather than a ?.



                    A mental model many people have for implicitly unwrapped optionals is that they are a type, distinct from regular optionals. In Swift 3, that was exactly how they worked: declarations like var a: Int? would result in a having type Optional<Int>, and declarations like var b: String! would result in b having type ImplicitlyUnwrappedOptional<String>.



                    The new mental model for IUOs is one where you consider ! to be a synonym for ? with the addition that it adds a flag on the declaration letting the compiler know that the declared value can be implicitly unwrapped.




                    In your case, specifically, you are printing an implicitly unwrapped optional, which is really just a fancy optional, so it’s expected & correct behavior. If you want to print the string value itself, you’ll need to explicitly unwrap the value, print(someObject.someProperty!). The linked blog post deep dives into the new reimplementation of IUOs & is worth a skim.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 8 at 7:24









                    Chris ZielinskiChris Zielinski

                    12




                    12





















                        0














                        Implicitly unwrapped optional is still optional. It just doesn’t look like that when you’re writing your code. So this property still can hold no value.




                        But for your specific need there is no reason to having implicitly unwrapped optional. Make it either non-optional or optional. Also you can use struct instead of class and then you can use default memberwise intializer



                        struct Model 
                        var property: String


                        let object = Model(property: "text")
                        print(object.property)



                        text







                        share|improve this answer



























                          0














                          Implicitly unwrapped optional is still optional. It just doesn’t look like that when you’re writing your code. So this property still can hold no value.




                          But for your specific need there is no reason to having implicitly unwrapped optional. Make it either non-optional or optional. Also you can use struct instead of class and then you can use default memberwise intializer



                          struct Model 
                          var property: String


                          let object = Model(property: "text")
                          print(object.property)



                          text







                          share|improve this answer

























                            0












                            0








                            0







                            Implicitly unwrapped optional is still optional. It just doesn’t look like that when you’re writing your code. So this property still can hold no value.




                            But for your specific need there is no reason to having implicitly unwrapped optional. Make it either non-optional or optional. Also you can use struct instead of class and then you can use default memberwise intializer



                            struct Model 
                            var property: String


                            let object = Model(property: "text")
                            print(object.property)



                            text







                            share|improve this answer













                            Implicitly unwrapped optional is still optional. It just doesn’t look like that when you’re writing your code. So this property still can hold no value.




                            But for your specific need there is no reason to having implicitly unwrapped optional. Make it either non-optional or optional. Also you can use struct instead of class and then you can use default memberwise intializer



                            struct Model 
                            var property: String


                            let object = Model(property: "text")
                            print(object.property)



                            text








                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 8:49









                            Robert DreslerRobert Dresler

                            8,0522727




                            8,0522727



























                                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%2f55058096%2fnon-optional-string-automatically-getting-changed-to-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

                                Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                                Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                                Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved