14
path = Bundle.main.path(forResource: "Owl.jpg", ofType: "jpg")

returns nil, however, using NSHomeDirectory() I'm able to verify that is under Documents/ folder.

2
  • 4
    your Bundle is what you see on the left in Xcode. Documents folder is actually created inside your app on device or simulator so it will never find this file. Commented Nov 14, 2016 at 22:40
  • @repoguy, that helps thank you. Can someone explain the downvote? Commented Nov 14, 2016 at 23:04

3 Answers 3

32

First, separate name and extension:

Bundle.main.path(forResource: "Owl", ofType: "jpg")

Second, separate (mentally) your bundle and the Documents folder. They are two completely different things. If this file is the Documents folder, it absolutely is not in your main bundle! You probably want something like this:

let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("Owl.jpg")

Third, if Owl is an image asset in the asset catalog, then say

let im = UIImage(named:"Owl") // or whatever its name is
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you Matt! Is it possible to move the files in Documents to Bundle?
No, you cannot modify your bundle. You can copy from your bundle into the Documents folder but you cannot go the other way. Why would you even want to?
Bundle is mostly where images in xcassets would be is that correct? Basically am playing with bundle, but Owl which is an asset is not being found. I also have it as a separate image which I can find under Documents.
You cannot get the path of something in the assets catalog. Why would you need to? You have the asset. — There is also a Documents folder in your app's sandbox but I do not know in what sense you can "see" it.
No, it looks into the main (.main) bundle (Bundle). (At zero depth.) One point of the asset catalog is that there is no need for paths; you just use a thing's name, because the runtime knows where the asset catalog is and how to read things by name within it. If you wanted this thing to be in your main bundle, then you shouldn't have put it in the asset catalog. Those are different places. I believe I've said this before. Perhaps more than once.
|
3

Tested on: xCode 8.3.2 & Swift 3.1

First drag your file (JPG, MP3, ZIP) inside your project folder and make sure Copy items if needed is checked and project/app is selected in Add to targetsenter image description here

Inside relevant ViewController

let fileName = "fileName"
let fileType = "fileType"

if let filePath = Bundle.main.path(forResource: fileName, ofType: fileType) {
    print(filePath)
}

If you need to get the file URL you can use NSBundle method

if let fileURL = Bundle.main.url(forResource: fileName, withExtension: fileType) {
    print(fileURL)
}

Also NSBundle method pathForResource has an initializer that you can specify in which directory your files are located like:

if let filePath = Bundle.main.path(forResource: fileName, ofType: fileType, inDirectory: "filesSubDirectory") {
    print(filePath)
}

And for getting file URL:

if let fileURL = Bundle.main.url(forResource: fileName, withExtension: fileType, subdirectory: "filesSubDirectory") {
    print(fileURL)
}

Comments

-1
enter code here

 let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
 let documentsDirectory = paths[0]
 print(documentsDirectory)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.