Arseniy Ermilov
Arseniy Ermilov

Reputation: 35

URLforResource always returns nil

I made a directory near AppDelegate.swift called files and put a first.png image there.

I ran this code

let fileURL = NSBundle.mainBundle().URLForResource("first", withExtension: "png", subdirectory: "files")

and fileURL turned out to be nil. I tried to upload image in Assets.xcassets but it returns nil too .

What the problem ?

Upvotes: 2

Views: 934

Answers (1)

jrturton
jrturton

Reputation: 119242

The subdirectory argument assumes that you've added files as a folder reference (it will show blue in Xcode) and not a group (it shows yellow). This option comes up when you drag the folder into Xcode to add it.

If it's a group, then anything inside it will be added to the bundle resources at the top level.

You can check where the file has ended up by looking at the package contents of your built project, there will be a folder called "Resources" which will contain all of your resources.

Upvotes: 2

Related Questions