Found nil while unwrapping optional - setting label text in Swift 4 [duplicate] - swift

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

Related

Unwrapping an Optional value for a Label [duplicate]

This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 2 years ago.
in this issue i tried to unwrap it by adding ! next verifiedLabel in viewDidLoad to become verifiedLabel!.text but the result is still printing Fatal error: Unexpectedly found nil while unwrapping an Optional value. Thank you
override func viewDidLoad() {
super.viewDidLoad()
verifiedLabel.text = "" }
self.verifiedLabel.text = user.isPhoneVerified ? "Verified" : "Not Verified" }
You are force unwrapping an optional by adding a !, so if the value is null like it is in your case the program will crash.
To unwrap an optional in a safe way (for a generic case) follow this method from Hacking With Swift:
var name: String? = nil
if let unwrapped = name {
print("\(unwrapped.count) letters")
} else {
print("Missing name.")
}
In your SPECIFIC case however verifiedLabel is likely your label set on a storyboard. So you can unwrap it like var verifiedLabel:UILabel! since the value should NOT be null.
So now since its null, a) please check your outlet connections and if everything looks good b) check this thread for debugging: IBOutlet is nil, but it is connected in storyboard, Swift

Want to set Slidervalue to a UserDefault: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value [duplicate]

This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 2 years ago.
My problem is that I want to set the value of a slider to a UserDefault. In the first version of the app everything worked fine and I use the same code in version 2. Now Xcode shows the error "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value", but I never have used Optionals in the code. I have updated Xcode, maybe there is a problem.
Thank you in advance
func speichernNutzerEinstellungenIntervall(){
standard.set(zeitInsgesamt, forKey: Keys.speichernZeitInsgesamt)
}
func überprüfenLängeIntervall(){
let speichernZeitInsgesamt = standard.integer(forKey: Keys.speichernZeitInsgesamt)
zeitInsgesamt = speichernZeitInsgesamt
sliderIntervallOutlet.setValue(Float(zeitInsgesamt), animated: true) //Here I get the error}
(I have declared the variable "zeitInsgesamt" as a global variable. Don't know if this is important.)
The value of sliderIntervallOutlet is nil. Try to check nil value for property before set any value. Like this
func überprüfenLängeIntervall(){
let speichernZeitInsgesamt = standard.integer(forKey: Keys.speichernZeitInsgesamt)
zeitInsgesamt = speichernZeitInsgesamt
if sliderIntervallOutlet != nil {
sliderIntervallOutlet.setValue(Float(zeitInsgesamt), animated: true)
}
}

crashing found nil while unwrapping optional value [duplicate]

This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 6 years ago.
Hi all I'm creating a chat app, using firebase.
when I get to the screen to create a channel it crashes saying found nil while unwrapping optional value. If i go back into the app the channel has been created so i presume it is finding nil when changing viewcontrolers and there must be nothing in the database for the new channel under messages. below is the code and where it crashes.
var channelRef: FIRDatabaseReference?
private lazy var messageRef: FIRDatabaseReference = self.channelRef!.child("messages")
then it crashes here...
private func observeMessages() {
messageRef = channelRef!.child("messages")
this function is called on view did load
Instead of force unwraps you should use and if let like this:
if let channelRef = channelRef {
messageRef = channelRef.child("messages")
} //maybe add an else and do some logic if value is not there
Or you could declare var channelRef: FIRDatabaseReference! if you expect it to always be there and be of this type

UISliders and setting values [duplicate]

This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 1 year ago.
I have a code I am writing and its going well but I am having trouble with UISliders...specifically...setting values. I have used the sliders and now I want to reset them to the original state (0 to 10 with a value of 0 and the slider all the way to the left. I have...
#IBOutlet weak var redHorizontalSlider: UISlider!
Inside viewDidLoad:
redHorizontalSlider!.minimumValue = 0.0
redHorizontalSlider!.maximumValue = 1.0
redHorizontalSlider!.setValue(0.0, animated: false)
and when I print the values its giving me a nil and an error:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
I have played around with ! and ? in the declaration of the Object and the calls but to no avail.
This error is going to happen because the value of your slider is nil. Check your connections to make sure that your storyboard slider is connected to the slider variable.

Swift : Unexpectedly found nil [duplicate]

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.