Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 620 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swift - How do I get the file path inside a folder

#1
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
Reply

#2
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.

[![enter image description here][1]][1]

[![enter image description here][2]][2]

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)
}


[1]:

[2]:
Reply

#3
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`

[![enter image description here][1]][1]

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!

[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through