0

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

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
potato
  • 4,479
  • 7
  • 42
  • 99
  • unable to get the resource. so pass a proper path . right click on the 1g.jpg and show in finder and then paste the path after the project name in the "inDirectory: ????" ? mark position – Fatti Khan Nov 12 '15 at 09:24
  • My complete path: file:///Users/user/Desktop/MattNeuburg/pathFinder/pathFinder/1g.jpg I tried pathFinder/1g.jpg, pathFinder, pathFinder/pathFinder. None worked:/ – potato Nov 12 '15 at 09:36
  • would you share your project via dropbox or something so i can take a look? – André Slotta Nov 12 '15 at 10:02
  • just figured it out.. – potato Nov 12 '15 at 10:17

2 Answers2

1

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

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • @krompir2 -- your coding is correct, check once your directory is correct or not , I given the reference link check once , you get some idea – Anbu.Karthik Nov 12 '15 at 09:34
  • I checked the link and nope, it still wont work. I also used this approach: let resource2 = NSBundle(forClass: self.dynamicType).pathForResource("1g", ofType: "jpg", inDirectory: "CustomAssets/Level1/") – potato Nov 12 '15 at 09:49
  • ya correct , but not get the answer , ok do like initially check all subdirectories and then put the above lines – Anbu.Karthik Nov 12 '15 at 09:52
  • look like groups havee no effect on actual path – potato Nov 12 '15 at 10:17
  • ha ha, Ok in your update question where.in which line get the error – Anbu.Karthik Nov 12 '15 at 10:21
  • then in your update question you show like **This is screenshot of my bundle and also the error I am getting:** , this means – Anbu.Karthik Nov 12 '15 at 10:23
0

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.

potato
  • 4,479
  • 7
  • 42
  • 99