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;
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?
ios swift swift-playground
add a comment |
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?
ios swift swift-playground
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 makingsomeProperty
avar
instead of initializing it as alet
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 aninit
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
add a comment |
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?
ios swift swift-playground
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?
ios swift swift-playground
ios swift swift-playground
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 makingsomeProperty
avar
instead of initializing it as alet
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 aninit
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
add a comment |
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 makingsomeProperty
avar
instead of initializing it as alet
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 aninit
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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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 ina
having typeOptional<Int>
, and declarations likevar b: String!
would result inb
having typeImplicitlyUnwrappedOptional<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.
add a comment |
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
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 8 at 6:57
NSCoderNSCoder
13813
13813
add a comment |
add a comment |
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 ina
having typeOptional<Int>
, and declarations likevar b: String!
would result inb
having typeImplicitlyUnwrappedOptional<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.
add a comment |
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 ina
having typeOptional<Int>
, and declarations likevar b: String!
would result inb
having typeImplicitlyUnwrappedOptional<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.
add a comment |
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 ina
having typeOptional<Int>
, and declarations likevar b: String!
would result inb
having typeImplicitlyUnwrappedOptional<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.
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 ina
having typeOptional<Int>
, and declarations likevar b: String!
would result inb
having typeImplicitlyUnwrappedOptional<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.
answered Mar 8 at 7:24
Chris ZielinskiChris Zielinski
12
12
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 8 at 8:49
Robert DreslerRobert Dresler
8,0522727
8,0522727
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%2f55058096%2fnon-optional-string-automatically-getting-changed-to-optional%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
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
avar
instead of initializing it as alet
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