Swift Error - fatal error: unexpectedly found nil while unwrapping an Optional value
Try this way:
if let uploadImage = UIImage(data: uploadData)!{
cell.attachedImage.image = uploadImage
}
This error occurs because your uploadData
is become nil at run time so check your code and find out yourself that why uploadData
is become nil at run time.
This code will not give you any error if your uploadData
will be nil.
EDIT:
If you get this error on first line then you can do like this way :
if let uploadFile = tweet["uploadedPic"] as? PFFile {
// Your code
}
Can't find error-Fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)
if let RPUserBackground = currentRPUser!["userBackground"] as? PFFile {
RPUserBackground.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
// Set user's background Photo
if let imageData = imageData {
/*
let image = UIImage(data: imageData)
let size = CGSize(width: image!.size.width / 2, height: image!.size.height / 2)
self.RPUserProPic.image = image
self.RPUserProPic.contentMode = .ScaleAspectFit
*/
self.RPUserBackground.image = UIImage(data: imageData)
self.RPDefaultView.hidden = true
self.RPUserBiography.textColor = UIColor.whiteColor()
}
}
}
} else {
print("User does not have a preferred background photo")
self.RPUserBackground.hidden = true
self.RPUserBiography.textColor = UIColor.grayColor() // GET RBG COLOR LATER
self.RPDefaultView.hidden = false
}
fatal error: unexpectedly found nil while unwrapping an Optional value (Swift & Parse)
You need to use conditional un-wrapping for the image object also.
Replace:
let imageData = UIImageJPEGRepresentation(image, 0.75)
let imageFile = PFFile(name: "image.png", data: imageData)
object.setValue(imageFile, forKey: "imageFile")
With:
if let image = image
{
let imageData = UIImageJPEGRepresentation(image, 0.75)
let imageFile = PFFile(name: "image.png", data: imageData)
object.setValue(imageFile, forKey: "imageFile")
}
unexpectedly found nil while unwrapping an Optional value dispatch-async
You use a lot of exclamation marks to force unwrap optional values in your code, it's a bad habit.
For example, you can unwrap guestname.last safely by:
guard let lastItem = guestname.last else {
// do something else
return
}
query.whereKey("username", equalTo: lastItem)
fatal error: unexpectedly found nil while unwrapping an Optional value (Swift)
This code is working fine with your URL:
let url = "http://files.parsetfss.com/461a4eda-d153-4d46-bd85-28ddd355a94c/tfss-03d4eb57-51cb-424d-8c90-2d8a89429203-00255--How_To_Build_A_Loving_Family.mp3"
let soundData = NSData(contentsOfURL: NSURL(string: url)!)
var error: NSError?
self.audioPlayer = AVAudioPlayer(data: soundData, error: &error)
if audioPlayer == nil
{
if let e = error
{
println(e.localizedDescription)
}
}
audioPlayer!.volume = 1.0
audioPlayer!.prepareToPlay()
audioPlayer!.play()
This way you can convert your audio to data which will take some time to play your audio.
Here is another way to play song live:
let url = audioFile.url!
let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
player = AVPlayer(playerItem:playerItem)
player.rate = 1.0;
player.play()
retrieve an optional value swift
Probably the place where the crash happens is in this line:
if let finalimage = UIImage(data: imageData!) {
because you're using the forced unwrapping operator. I would add a quick check to the preceding if
statement to check for not nil:
if error == nil && imageData != nil {
or even better use optional binding:
if let imageData = imageData where error == nil {
if let finalimage = UIImage(data: imageData) {
self.avatarImage.image = finalimage
}
}
Update: if the error happens in the first line, then you should (again) use optional binding to protect against undesired crashes:
if let avatarFile = PFUser.currentUser()?["Avatar"] as? PFFile {
}
Related Topics
Differences Between Ways of Initializing a Dictionary
Don't Delete Some Rows from Uitableview
How to Read Ansi Escape Code Response Value in Swift
How to Handle Two Possible Date Formats
Xcode 7.1 Beta: Content of File Error
Failable Initializers with Codable
Var Declaration with Type VS Without
Why Reloaddata Is Showing Error for My Tableview Inside Viewcontroller
Auth.Auth().Currentuser.Reload() Doesn't Refresh Currentuser.Isemailverified
Swiftui How to Programatically Adjust Spacing Between Images and Text
Stanford Calculator App Keeps Crashing
Any Way to Get a Gif as a Background with Swiftui
How to Pass Closure as a Parameter in Perform(Selector, Withobject)