This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 5 years ago.
I don't understand why it is coming.
Are you sure that object for key "quantity" is array of Doubles? You force unwrapping results so make sure that it is [Double]
Try this
guard let quantity: [Double] = salesByStateResponse.value(forKey: "quantity") as? [Double] else {
return
}
Related
This question already has answers here:
Please Help Me Intepret This Code SWIFT
(4 answers)
What is the `?` is or the `:` at the equation when coding in Swift
(3 answers)
Closed 4 years ago.
Can someone help me understand the following code expression, how would you read it?
let myVar = (someOptional != nil) ? someOptional! : ""
If someOptional is not nil unwrap it? if this is correct, what does do the ? and the :"" in the expression?
I have always unwrapped my optionals with if lets but and I'm not sure how the above code reads.
Here is a more concrete real code example that shows how it is used...
let currentZipCode = (placemark.postalCode != nil) ? placemark.postalCode! : ""
This question already has an answer here:
Constant unassigned optional will not be nil by default
(1 answer)
Closed 4 years ago.
let errorCodestring : String?
if let theError = errorCodestring {
print(theError)
}
In the above code Xcode throws the error:
constant 'errorCodestring'being used before being initialized
What is wrong in the code and how to clear the error?
You have to initialize the constant before using it.
let errorCodestring : String? = nil
This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 4 years ago.
I have the following code using Facebook's Swift API:
let displayName = user.displayName
print(displayName)
self.nameLabel.text = displayName
The displayname variable is printed correctly but when it reaches the last line, I get Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. But displayName has a value?
This might be due to nameLabel being nil. Make sure the IBOutlet is connected properly.
If the nameLabel can be nil, avoid the crash using this:
self.nameLabel?.text = displayName
This question already has answers here:
What is an optional value in Swift?
(15 answers)
Closed 5 years ago.
How can I get the string value in Optional() in swift, which is followed by nil?any other proper way to use this evaluateJavaScript() and get the callback value?
webView.evaluateJavaScript("(function() { return 'this'; })();",
completionHandler: {
result in
//Handle your variable
print(result)//(Optional(this), nil)
})
Use if let to parse the optional value:
if let value = result {
print(value)
}
This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Fatal error: unexpectedly found nil while unwrapping an Optional values [duplicate]
(13 answers)
Closed 6 years ago.
This code is causing my application to crash and I can't figure out a way to fix it. The error is : fatal error: unexpectedly found nil while unwrapping an Optional value
Any idea how I can fix it? The two lines I'm returning in my createCharacters() function is whats causing the crash:
class NACharacters {
var featuredImage : UIImage!
init(featuredImage: UIImage){
self.featuredImage = featuredImage
}
static func createCharacters() -> [NACharacters]{
return[
//THE TWO LINES BELOW CAUSE THE CRASH
NACharacters(featuredImage: UIImage(named: "Diplo Squad")!),
NACharacters(featuredImage: UIImage(named: "StopIcon")!)
]
}
}
Solution: I simply needed to delete the space between "Diplo" and "Squad". It seems this was returning nil.
check if your images "Diplo Squad" und "StopIcon" exist.
(You may need to remove the space in the first image name)
At least one of these UIImage(name: "...") calls returns nil and that's probably the crash reason.
The only thing that could be nil lines are in the UIImages. check that they exist in your projectNavigator or assets.