Unexpectedly found nil while unwrapping an Optional value but value exist
I know I need to bind my varaible for unwrap but the problem is my value is not reconized but present.
This is my code :
surveyW.karmaWin = Int(endedSurvey["karma"].string!)
endedSurvey
is a array dictionary of my JSON
backend. I get a Unexpectedly found nil while unwrapping an Optional value
error.
I specify that I force the unwrapping to show you my problem.
The problem is my array contains the karma
value. I show you the screen of the value:
So we can see that the value existing. Why I get a Unexpectedly found nil...
?
Thanks to your reply!
swift swifty-json
|
show 2 more comments
I know I need to bind my varaible for unwrap but the problem is my value is not reconized but present.
This is my code :
surveyW.karmaWin = Int(endedSurvey["karma"].string!)
endedSurvey
is a array dictionary of my JSON
backend. I get a Unexpectedly found nil while unwrapping an Optional value
error.
I specify that I force the unwrapping to show you my problem.
The problem is my array contains the karma
value. I show you the screen of the value:
So we can see that the value existing. Why I get a Unexpectedly found nil...
?
Thanks to your reply!
swift swifty-json
3
If endedSurvey is an array you need to access the element first and then it's key endedSurvey[1] will have key value pair of "karma" not endedSurvey.
– Mukul More
Nov 22 '18 at 8:04
2
Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?
– Kamran
Nov 22 '18 at 8:13
@MukulMore it is obvious from screenshot that it's aDictionary
, not anArray
.
– inokey
Nov 22 '18 at 8:14
2
I would recommend to switch from SwiftyJSON toDecodable
.
– Sulthan
Nov 22 '18 at 8:26
1
But if you want to keep using SwiftyJSON, at least please use it the way it's intended:.string
is the optional getter, whereas.stringValue
is the non-optional getter. You should not force unwrap.string
yourself. Works with other types: .int vs .intValue, etc.
– ayaio
Nov 22 '18 at 10:07
|
show 2 more comments
I know I need to bind my varaible for unwrap but the problem is my value is not reconized but present.
This is my code :
surveyW.karmaWin = Int(endedSurvey["karma"].string!)
endedSurvey
is a array dictionary of my JSON
backend. I get a Unexpectedly found nil while unwrapping an Optional value
error.
I specify that I force the unwrapping to show you my problem.
The problem is my array contains the karma
value. I show you the screen of the value:
So we can see that the value existing. Why I get a Unexpectedly found nil...
?
Thanks to your reply!
swift swifty-json
I know I need to bind my varaible for unwrap but the problem is my value is not reconized but present.
This is my code :
surveyW.karmaWin = Int(endedSurvey["karma"].string!)
endedSurvey
is a array dictionary of my JSON
backend. I get a Unexpectedly found nil while unwrapping an Optional value
error.
I specify that I force the unwrapping to show you my problem.
The problem is my array contains the karma
value. I show you the screen of the value:
So we can see that the value existing. Why I get a Unexpectedly found nil...
?
Thanks to your reply!
swift swifty-json
swift swifty-json
edited Nov 22 '18 at 9:00
El-Burritos
asked Nov 22 '18 at 8:02
El-BurritosEl-Burritos
139211
139211
3
If endedSurvey is an array you need to access the element first and then it's key endedSurvey[1] will have key value pair of "karma" not endedSurvey.
– Mukul More
Nov 22 '18 at 8:04
2
Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?
– Kamran
Nov 22 '18 at 8:13
@MukulMore it is obvious from screenshot that it's aDictionary
, not anArray
.
– inokey
Nov 22 '18 at 8:14
2
I would recommend to switch from SwiftyJSON toDecodable
.
– Sulthan
Nov 22 '18 at 8:26
1
But if you want to keep using SwiftyJSON, at least please use it the way it's intended:.string
is the optional getter, whereas.stringValue
is the non-optional getter. You should not force unwrap.string
yourself. Works with other types: .int vs .intValue, etc.
– ayaio
Nov 22 '18 at 10:07
|
show 2 more comments
3
If endedSurvey is an array you need to access the element first and then it's key endedSurvey[1] will have key value pair of "karma" not endedSurvey.
– Mukul More
Nov 22 '18 at 8:04
2
Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?
– Kamran
Nov 22 '18 at 8:13
@MukulMore it is obvious from screenshot that it's aDictionary
, not anArray
.
– inokey
Nov 22 '18 at 8:14
2
I would recommend to switch from SwiftyJSON toDecodable
.
– Sulthan
Nov 22 '18 at 8:26
1
But if you want to keep using SwiftyJSON, at least please use it the way it's intended:.string
is the optional getter, whereas.stringValue
is the non-optional getter. You should not force unwrap.string
yourself. Works with other types: .int vs .intValue, etc.
– ayaio
Nov 22 '18 at 10:07
3
3
If endedSurvey is an array you need to access the element first and then it's key endedSurvey[1] will have key value pair of "karma" not endedSurvey.
– Mukul More
Nov 22 '18 at 8:04
If endedSurvey is an array you need to access the element first and then it's key endedSurvey[1] will have key value pair of "karma" not endedSurvey.
– Mukul More
Nov 22 '18 at 8:04
2
2
Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?
– Kamran
Nov 22 '18 at 8:13
Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?
– Kamran
Nov 22 '18 at 8:13
@MukulMore it is obvious from screenshot that it's a
Dictionary
, not an Array
.– inokey
Nov 22 '18 at 8:14
@MukulMore it is obvious from screenshot that it's a
Dictionary
, not an Array
.– inokey
Nov 22 '18 at 8:14
2
2
I would recommend to switch from SwiftyJSON to
Decodable
.– Sulthan
Nov 22 '18 at 8:26
I would recommend to switch from SwiftyJSON to
Decodable
.– Sulthan
Nov 22 '18 at 8:26
1
1
But if you want to keep using SwiftyJSON, at least please use it the way it's intended:
.string
is the optional getter, whereas .stringValue
is the non-optional getter. You should not force unwrap .string
yourself. Works with other types: .int vs .intValue, etc.– ayaio
Nov 22 '18 at 10:07
But if you want to keep using SwiftyJSON, at least please use it the way it's intended:
.string
is the optional getter, whereas .stringValue
is the non-optional getter. You should not force unwrap .string
yourself. Works with other types: .int vs .intValue, etc.– ayaio
Nov 22 '18 at 10:07
|
show 2 more comments
3 Answers
3
active
oldest
votes
The value contained in "karma"
is not String
. You're trying to force cast it with SwiftyJSON but it tells you it has a nil. You first need to extract value as it is - .int
, and after that convert that to something else if needed.
surveyW.karmaWin = endedSurvey["karma"].int
add a comment |
endedSurvey["karma"] is an Integer not string and also good way to unwrap an optional is:
if let karma = endedSurvey["karma"] as? Int{
surveyW.karmaWin = karma
}
This is SwiftyJSON
– Sulthan
Nov 22 '18 at 8:26
1
I have given a reply of native feature. Also i want to say that please don't rely on these third party libraries. Native features of apple are awesome and fast.
– Pratyush Pratik
Nov 22 '18 at 8:28
3
SwiftyJSON is a decent piece of software that helped a lot beforeCodable
became available in Swift 4. Some use case are easier to write in SwiftyJSON than usingCodable
. Anyway, this question is about SwiftyJSON, therefore answer telling "don't use SwiftyJSON" won't be welcome.
– Sulthan
Nov 22 '18 at 8:31
add a comment |
You can use intValue because SwiftyJSON has two kinds of "getters" for retrieving values: Optional and non-Optional
.string and .int are the optional getters for the String and Int representation of a value, so you have to unwrap it before use
if let fbId = fbJson["id"].string {
print(fbId)
}
If you are 100% sure that there will always be a value, you can use the equivalent of "force unwrap" by using the non-Optional getter and you don't need if let anymore:
let fbId = fbJson["id"].stringValue
In your code :
surveyW.karmaWin = endedSurvey["karma"].intValue
This work, but what's different between .int and .intValue ?
– El-Burritos
Nov 22 '18 at 9:08
1
.int returns an optional int, which is nil if the value is not a real int. And .intValue provides an int no matter what, which means is tries to convert other values to int values and if that's not possible then it returns a 0.
– alireza kiani
Nov 22 '18 at 20:52
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%2f53426298%2funexpectedly-found-nil-while-unwrapping-an-optional-value-but-value-exist%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
The value contained in "karma"
is not String
. You're trying to force cast it with SwiftyJSON but it tells you it has a nil. You first need to extract value as it is - .int
, and after that convert that to something else if needed.
surveyW.karmaWin = endedSurvey["karma"].int
add a comment |
The value contained in "karma"
is not String
. You're trying to force cast it with SwiftyJSON but it tells you it has a nil. You first need to extract value as it is - .int
, and after that convert that to something else if needed.
surveyW.karmaWin = endedSurvey["karma"].int
add a comment |
The value contained in "karma"
is not String
. You're trying to force cast it with SwiftyJSON but it tells you it has a nil. You first need to extract value as it is - .int
, and after that convert that to something else if needed.
surveyW.karmaWin = endedSurvey["karma"].int
The value contained in "karma"
is not String
. You're trying to force cast it with SwiftyJSON but it tells you it has a nil. You first need to extract value as it is - .int
, and after that convert that to something else if needed.
surveyW.karmaWin = endedSurvey["karma"].int
answered Nov 22 '18 at 8:12
inokeyinokey
1,100818
1,100818
add a comment |
add a comment |
endedSurvey["karma"] is an Integer not string and also good way to unwrap an optional is:
if let karma = endedSurvey["karma"] as? Int{
surveyW.karmaWin = karma
}
This is SwiftyJSON
– Sulthan
Nov 22 '18 at 8:26
1
I have given a reply of native feature. Also i want to say that please don't rely on these third party libraries. Native features of apple are awesome and fast.
– Pratyush Pratik
Nov 22 '18 at 8:28
3
SwiftyJSON is a decent piece of software that helped a lot beforeCodable
became available in Swift 4. Some use case are easier to write in SwiftyJSON than usingCodable
. Anyway, this question is about SwiftyJSON, therefore answer telling "don't use SwiftyJSON" won't be welcome.
– Sulthan
Nov 22 '18 at 8:31
add a comment |
endedSurvey["karma"] is an Integer not string and also good way to unwrap an optional is:
if let karma = endedSurvey["karma"] as? Int{
surveyW.karmaWin = karma
}
This is SwiftyJSON
– Sulthan
Nov 22 '18 at 8:26
1
I have given a reply of native feature. Also i want to say that please don't rely on these third party libraries. Native features of apple are awesome and fast.
– Pratyush Pratik
Nov 22 '18 at 8:28
3
SwiftyJSON is a decent piece of software that helped a lot beforeCodable
became available in Swift 4. Some use case are easier to write in SwiftyJSON than usingCodable
. Anyway, this question is about SwiftyJSON, therefore answer telling "don't use SwiftyJSON" won't be welcome.
– Sulthan
Nov 22 '18 at 8:31
add a comment |
endedSurvey["karma"] is an Integer not string and also good way to unwrap an optional is:
if let karma = endedSurvey["karma"] as? Int{
surveyW.karmaWin = karma
}
endedSurvey["karma"] is an Integer not string and also good way to unwrap an optional is:
if let karma = endedSurvey["karma"] as? Int{
surveyW.karmaWin = karma
}
answered Nov 22 '18 at 8:21
Pratyush PratikPratyush Pratik
511410
511410
This is SwiftyJSON
– Sulthan
Nov 22 '18 at 8:26
1
I have given a reply of native feature. Also i want to say that please don't rely on these third party libraries. Native features of apple are awesome and fast.
– Pratyush Pratik
Nov 22 '18 at 8:28
3
SwiftyJSON is a decent piece of software that helped a lot beforeCodable
became available in Swift 4. Some use case are easier to write in SwiftyJSON than usingCodable
. Anyway, this question is about SwiftyJSON, therefore answer telling "don't use SwiftyJSON" won't be welcome.
– Sulthan
Nov 22 '18 at 8:31
add a comment |
This is SwiftyJSON
– Sulthan
Nov 22 '18 at 8:26
1
I have given a reply of native feature. Also i want to say that please don't rely on these third party libraries. Native features of apple are awesome and fast.
– Pratyush Pratik
Nov 22 '18 at 8:28
3
SwiftyJSON is a decent piece of software that helped a lot beforeCodable
became available in Swift 4. Some use case are easier to write in SwiftyJSON than usingCodable
. Anyway, this question is about SwiftyJSON, therefore answer telling "don't use SwiftyJSON" won't be welcome.
– Sulthan
Nov 22 '18 at 8:31
This is SwiftyJSON
– Sulthan
Nov 22 '18 at 8:26
This is SwiftyJSON
– Sulthan
Nov 22 '18 at 8:26
1
1
I have given a reply of native feature. Also i want to say that please don't rely on these third party libraries. Native features of apple are awesome and fast.
– Pratyush Pratik
Nov 22 '18 at 8:28
I have given a reply of native feature. Also i want to say that please don't rely on these third party libraries. Native features of apple are awesome and fast.
– Pratyush Pratik
Nov 22 '18 at 8:28
3
3
SwiftyJSON is a decent piece of software that helped a lot before
Codable
became available in Swift 4. Some use case are easier to write in SwiftyJSON than using Codable
. Anyway, this question is about SwiftyJSON, therefore answer telling "don't use SwiftyJSON" won't be welcome.– Sulthan
Nov 22 '18 at 8:31
SwiftyJSON is a decent piece of software that helped a lot before
Codable
became available in Swift 4. Some use case are easier to write in SwiftyJSON than using Codable
. Anyway, this question is about SwiftyJSON, therefore answer telling "don't use SwiftyJSON" won't be welcome.– Sulthan
Nov 22 '18 at 8:31
add a comment |
You can use intValue because SwiftyJSON has two kinds of "getters" for retrieving values: Optional and non-Optional
.string and .int are the optional getters for the String and Int representation of a value, so you have to unwrap it before use
if let fbId = fbJson["id"].string {
print(fbId)
}
If you are 100% sure that there will always be a value, you can use the equivalent of "force unwrap" by using the non-Optional getter and you don't need if let anymore:
let fbId = fbJson["id"].stringValue
In your code :
surveyW.karmaWin = endedSurvey["karma"].intValue
This work, but what's different between .int and .intValue ?
– El-Burritos
Nov 22 '18 at 9:08
1
.int returns an optional int, which is nil if the value is not a real int. And .intValue provides an int no matter what, which means is tries to convert other values to int values and if that's not possible then it returns a 0.
– alireza kiani
Nov 22 '18 at 20:52
add a comment |
You can use intValue because SwiftyJSON has two kinds of "getters" for retrieving values: Optional and non-Optional
.string and .int are the optional getters for the String and Int representation of a value, so you have to unwrap it before use
if let fbId = fbJson["id"].string {
print(fbId)
}
If you are 100% sure that there will always be a value, you can use the equivalent of "force unwrap" by using the non-Optional getter and you don't need if let anymore:
let fbId = fbJson["id"].stringValue
In your code :
surveyW.karmaWin = endedSurvey["karma"].intValue
This work, but what's different between .int and .intValue ?
– El-Burritos
Nov 22 '18 at 9:08
1
.int returns an optional int, which is nil if the value is not a real int. And .intValue provides an int no matter what, which means is tries to convert other values to int values and if that's not possible then it returns a 0.
– alireza kiani
Nov 22 '18 at 20:52
add a comment |
You can use intValue because SwiftyJSON has two kinds of "getters" for retrieving values: Optional and non-Optional
.string and .int are the optional getters for the String and Int representation of a value, so you have to unwrap it before use
if let fbId = fbJson["id"].string {
print(fbId)
}
If you are 100% sure that there will always be a value, you can use the equivalent of "force unwrap" by using the non-Optional getter and you don't need if let anymore:
let fbId = fbJson["id"].stringValue
In your code :
surveyW.karmaWin = endedSurvey["karma"].intValue
You can use intValue because SwiftyJSON has two kinds of "getters" for retrieving values: Optional and non-Optional
.string and .int are the optional getters for the String and Int representation of a value, so you have to unwrap it before use
if let fbId = fbJson["id"].string {
print(fbId)
}
If you are 100% sure that there will always be a value, you can use the equivalent of "force unwrap" by using the non-Optional getter and you don't need if let anymore:
let fbId = fbJson["id"].stringValue
In your code :
surveyW.karmaWin = endedSurvey["karma"].intValue
answered Nov 22 '18 at 8:26
alireza kianialireza kiani
12
12
This work, but what's different between .int and .intValue ?
– El-Burritos
Nov 22 '18 at 9:08
1
.int returns an optional int, which is nil if the value is not a real int. And .intValue provides an int no matter what, which means is tries to convert other values to int values and if that's not possible then it returns a 0.
– alireza kiani
Nov 22 '18 at 20:52
add a comment |
This work, but what's different between .int and .intValue ?
– El-Burritos
Nov 22 '18 at 9:08
1
.int returns an optional int, which is nil if the value is not a real int. And .intValue provides an int no matter what, which means is tries to convert other values to int values and if that's not possible then it returns a 0.
– alireza kiani
Nov 22 '18 at 20:52
This work, but what's different between .int and .intValue ?
– El-Burritos
Nov 22 '18 at 9:08
This work, but what's different between .int and .intValue ?
– El-Burritos
Nov 22 '18 at 9:08
1
1
.int returns an optional int, which is nil if the value is not a real int. And .intValue provides an int no matter what, which means is tries to convert other values to int values and if that's not possible then it returns a 0.
– alireza kiani
Nov 22 '18 at 20:52
.int returns an optional int, which is nil if the value is not a real int. And .intValue provides an int no matter what, which means is tries to convert other values to int values and if that's not possible then it returns a 0.
– alireza kiani
Nov 22 '18 at 20:52
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%2f53426298%2funexpectedly-found-nil-while-unwrapping-an-optional-value-but-value-exist%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
If endedSurvey is an array you need to access the element first and then it's key endedSurvey[1] will have key value pair of "karma" not endedSurvey.
– Mukul More
Nov 22 '18 at 8:04
2
Possible duplicate of What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?
– Kamran
Nov 22 '18 at 8:13
@MukulMore it is obvious from screenshot that it's a
Dictionary
, not anArray
.– inokey
Nov 22 '18 at 8:14
2
I would recommend to switch from SwiftyJSON to
Decodable
.– Sulthan
Nov 22 '18 at 8:26
1
But if you want to keep using SwiftyJSON, at least please use it the way it's intended:
.string
is the optional getter, whereas.stringValue
is the non-optional getter. You should not force unwrap.string
yourself. Works with other types: .int vs .intValue, etc.– ayaio
Nov 22 '18 at 10:07