let myPath = Bundle.main.path(forResource: "Settings", ofType: ".png")
print(myPath!)
Why does it crash when I'm trying to print this?
The crash is the famous Unexpected found nil while unwrapping ... error. Don't use exclamation marks unless it's guaranteed that the value is not nil.
Either the file does not exist or (most likely) your type (extension) is png not .png
let myPath = Bundle.main.path(forResource: "Settings", ofType: "png")
However nowadays the URL related API is preferable
let myURL = Bundle.main.url(forResource: "Settings", withExtension: "png")
My simple guess is that myPath is nil, so it crashes at the nil pointer exception. Remove the exclamation mark and use:
print(myPath)
If it prints nil, then you have your answer.
Related
I've had this bug where i use avfoundation to make a path to my audio file so when i use this line of code
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Alone", ofType: "m4r")!))
audioPlayer.prepareToPlay()
It crashes and gives me
fatal error: unexpectedly found nil while unwrapping an Optional value
I have all the code right as the compiler dosen't show any errors.
I am using Xcode 9 and swift 4
This happen because you do forced unwrapping of non-existed path. Generally it's a bad practice. Try to avoid forced unwrapping.
Try this:
guard let path = Bundle.main.path(forResource: "Alone", ofType: "m4r") else {
print("wrong path")
return
}
let url = URL(fileURLWithPath: path)
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.prepareToPlay()
Then if you see a "wrong path" in debug console that means the resource with filename "Alone" and extension "m4r" doesn't exist in your app bundle.
Hope this help.
I have a question on how to use the new error handling in Swift.
I'm reading contents of a file into a data object:
var overallData: Data?
//load file contents into data object
let dataFileURL = NSURL(string: fileName)
do {
overallData = try Data(contentsOf: dataFileURL as! URL)
} catch {
print("\(error)")
}
The problem is that I always encounter this error message:
fatal error: unexpectedly found nil while unwrapping an Optional value
The problem is that the overallData object is set as nil. But if I don't define a data variable outside the do-catch,
let dataFileURL = NSURL(string: fileName)
do {
overallData = try Data(contentsOf: dataFileURL as! URL)
} catch {
print("\(error)")
}
Later on, I can't use the overallData object because the system keeps telling me it's a variable not defined yet. So it looks like new variables defined in the do-catch loop can only be locally accessed inside the loop.
Do you know how to solve this problem? I do need to use the overallData object elsewhere.
The following answer assumes your error is with the line:
overallData = try Data(contentsOf: dataFileURL as! URL)
If you are getting the "fatal error" on another line, please update your question.
Your error has nothing to do with the do/catch/try.
Your problem is the force unwrapping of dataFileURL which is nil.
Your problem is this line:
let dataFileURL = NSURL(string: fileName)
This is returning nil because fileName isn't a valid URL.
Assuming fileName is a path to a local file, you need to do:
let dataFileURL = URL(fileURLWithPath: fileName)
Also note the use of URL instead of NSURL. There is no sense in using NSURL in Swift 3.
I ve just upgrade from Swift 2 to Swift 3, and i m facing a new challenge...
I have a player which run perfectly before, but now i have this following issue : "unexpectedly found nil while unwrapping an Optional value"
Here is my code :
print(audioselectionne)
let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: audioselectionne as String, ofType: "mp3")!)
I ve got : Optional("tiesto") and the crash...
I really dont understand where is the issue...
Thanks for the help.
You should unwrap the optional, perhaps with optional binding.
BTW, you shouldn't be use path strings at all anymore. Just use the URL directly, e.g.
guard let resource = audioselectionne, let alertSound = Bundle.main.url(forResource: resource, withExtension: "mp3") else {
// handle file not found here
return
}
// use alertSound here
I think the Bundle.main.path method returns an optional String. When that’s nil (because the resource was not found), force-unwrapping it causes your error. If you want to handle it correctly, you have to check for the nil:
guard let path = Bundle.main.path(…) else {
// resource not found, handle error
}
// now `path` is guaranteed to be non-nil
let alertSound = URL(fileURLWithPath: path)
var audioPath = NSURL(fileURLWithPath: Bundle.main.path(forResource: "vellipomaakey", ofType: "mp3")!).
fatal error: unexpectedly found nil while unwrapping an Optional value
Did you see it in the Copy Bundle Resources section? If not, press on + sign to add that mp3 file.
You are force unwrapping your optional, consider the following:
if let res = Bundle.main.path(forResource: "vellipomaakey", ofType: "mp3") {
var audioPath = NSURL(fileURLWithPath:res)
}
This will most likely take away your run time error but it won't solve your issue. The problem here is that the resource you are trying to load is not being found, so Bundle.main.path(forResource: "vellipomaakey, ofType: "mp3") is returning nil.
I am trying to locate a file in a subdirectory of subdirectory of my bundle. I used this code to create an UIImage with init(contentsOfFile:).
let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg", inDirectory: "Level1")
let image = UIImage(contentsOfFile: resource!)
What am I doing wrong?
EDIT:
This works because groups you create in xcode editor do not affect the actual path.
let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg", inDirectory: nil)
or let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg")
you are get the error fatal error: unexpectedly found nil while unwrapping an Optional value, this means your variable is set to nil, but your code is expecting it to not be nil.
do like check the resource contains value or not.
let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg", inDirectory: "Level1")
print (resource) //
then
do like
Choice -1
if let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg", inDirectory: "Level1")
{
let image = UIImage(contentsOfFile: resource!)
}
else
{
print (resource)
}
Choice -2
let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg", inDirectory: "Level1")
if resource != nil {
//Do Something
let image = UIImage(contentsOfFile: resource!)
}
for additional information
Ok I figured it out. Based on the path Fatti Khan mentioned I tried
let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg",inDirectory: nil)
which is the same as
let resource = NSBundle.mainBundle().pathForResource("1g", ofType: "jpg")
It seems that even though I created groups in xcode editor that had no effect no the actual path, which is: file:///Users/user/Desktop/MattNeuburg/pathFinder/pathFinder/1g.jpg. pathFinder is the name of the project.