Error called trying to get Bundle URL - ios

I am getting an error when I try to get a path of an audio file I have.
let path = Bundle.main.path(forResource: "SaveALife", ofType: "mp3")!
In the console I receive this:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
Any help? Thanks.

I will go with #Rob, You must have spelled the resource name incorrectly or the file is not there in the bundle.
And by providing "!" you are forcing to get string path, but as the file is not there Or due to spelling mismatch the file is not found in bundle, the return path would be nil, and due to "!" it tries to unwrap the nil which results the crash.
So the solution is remove the "!" like below
let path = Bundle.main.path(forResource: "SaveALife", ofType: "mp3")
Or else if you definitely want to use "!", You must have to give correct resource path and confirm that the resource must be there in the bundle.
Hope it helps.
Happy coding ...

Make sure SaveALife.mp3 should be in your bundle. Also when you drag and drop the file please check copy bundle resources.

Related

can't access ios bundle resource

I am adding my first .wav file to my iOS project using XCode 11.3.1. I have tried various ways to access this resource:
let fileURL = Bundle.main.url(forResource: "greetings.wav", withExtension: nil)
let fileURL = Bundle.main.url(forResource: "greetings", withExtension: "wav")
let fileURL = Bundle.main.path(forResource: "greetings", ofType: "wav")
In all of these cases, fileURL is nil. Other things I have tried:
When I click on the .wav in Project Navigator, I see that Target Membership is checked for my target.
I have ensured greetings.wav is in the same folder as my ViewController which is executing the above statement.
I have also tried both the "Copy to Group" and "Copy to Folder Reference" when adding this resource
Nothing as of yet has produced a non-nil fileURL. Any help/additional troubleshooting so that I can get a valid URL for my .wav file would be greatly appreciated! Thanks.
Check whether your file is available in Copy Bundle Resources under build settings
Just offering possible suggestions here:
Check if you have imported the AVFoundation Module
Check if your resource is called "greetings.wav"

Bundle.main.path(forResource:ofType:inDirectory:) returns nil when app name contains "space"

Why the Bundle.main.path(forResource: "testFile", ofType: "rtf") returns nil when the app's name has space? anyone encounter this? and perhaps able to fixed? The error doesn't occur if I remove the "space" in the app's name.
When I print the path the result is this /Users/<user>/Library/Developer/CoreSimulator/Devices/915D4A93-D812-4180-A49E-6BFA3BD77986/data/Containers/Bundle/Application/8623072B-7528-463C-971F-ECD1FB89BDDB/Test Application.app/testFile.rtf as you may notice the app's name "Test Application.app" has space on it. this causes the Bundle.main.path to return nil
It's highly recommended to use the URL related API anyway
Bundle.main.url(forResource: "testFile", withExtension: "rtf")
Swift 4.1
Its work with me to read the mp3 files in my app
Bundle.main.paths(forResourcesOfType: "mp3", inDirectory: "")

Error when reading in txt file to iOS app

Im currently working on an iOS app that requires the parsing of a dictionary of words.
When I attempt to import the file and convert the contents to Strings I get an error on the let path:String = Bundle.main.path(forResource: "words", ofType: "txt")! line. The error is Thread 1: "EXC_BREAKPOINT(code=1,subcode=0x1002e11ec)"
Any help would be greatly appreciated.
NOTE: Screen shot of assets attached
let path:String = Bundle.main.path(forResource: "words", ofType: "txt")!
text = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
words = text.components(separatedBy: ("\n"))
Bundle path(forResource:ofType:) returns an optional String. You are force unwrapping the options. This crashes when the return value is nil.
It returns nil when there is actually no such file in your app's resource bundle.
You need to actually have a file named words.txt in your app's bundle. Make sure the filename case matches and make sure the file is selected for your target.
Generally you shouldn't force-unwrap optionals. It causes crashes. However, one can argue that the two force-unwraps in the code you posted will never crash once your app is working properly. The first one is only crashing because you have not yet properly put the file in your resource bundle. And the 2nd (try!) won't crash once your file is correct because you know it will be a valid text file in the given encoding.
Don't use a dataset. Just drag your txt file right into the file navigator.
To explain further, the error you're seeing is that you're using ! which says you're guaranteeing that a path for that resource exists in the bundle. Which it doesn't.

Storing Sounds In a Folder (Swift File Path) [duplicate]

I have a set of audio files inside a folder. I am able to access the file when the file is placed at main bundle, but if the files are moved inside the folder I am not able to access the files.
Code:
let audioFileName:String = "audioFiles/" + String(index)
let audioFile = NSBundle.mainBundle().pathForResource(audioFileName, ofType: "mp3")!
I have an audio file inside the folder audioFiles and I would want to get its path.
Error:
fatal error: unexpectedly found nil while unwrapping an Optional value
First make sure when you drag your folder audioFiles to your project to check copy items if needed and select create folder references. Make sure it shows a blue folder if your project.
Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:
let audioFileName = "audioName"
if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {
print(audioFilePath)
}
If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)
if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {
print(audioFileURL)
}
The answer by Leo should work, the critical step is checking "Copy Items if Needed". However, what do you do if you already created a file or forgot that critical step?
Its easy to fix.
Go to Project -> Build Phases -> Copy Bundle Resources
Here you will see a handy list of all the files you've added to your bundle (including all your xcassets!)
Click the add button, and find your missing unlinked file.
Your code will now work (I built my own example, so it won't be same as yours in name):
if let url = Bundle.main.url(forResource: "testData2", withExtension: "txt") {
do {
let myData = try Data(contentsOf: url)
print(myData.count)
} catch {
print(error)
}
}
So why weren't we finding the file to begin with? Simple. We were asking the bundle. "Hey where is my file". Bundle was like... I don't know about that file, and if you checked the list of what was part of it (the Bundle Resources that are being copied) it wasn't there. taDa!

allowing NSBundle.mainBundle() in swift iOS app to find "der" format certificate files

I am extremely new to Swift iOS programming in xcode. For whatever reason, my NSBundle.mainBundle() is able to find image files in the code base,
var path = NSURL(fileURLWithPath: NSBundle.mainBundle()
.pathForResource("icon", ofType: "png")!)
returns a non-nil string.
However, I have a "DER" format public CA certificate in the same directory.
path = NSURL(fileURLWithPath: NSBundle.mainBundle()
.pathForResource("cacer", ofType: "der")!)
returns nil.
Any advice to give? Any extra information needed?
Looks like cancer.der isn't referenced to app target.
Make sure that checkbox is on for that file:

Resources