-2

I have a folder with a bunch of images named “Pictures” right inside “Supporting Files”, but when I try to retrieve the files it returns nil, even though I'm sure there are images inside; I try to do it with the following code inside viewDidLoad()

let sourcePath = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("Pictures")
let imgArray = NSFileManager.defaultManager().contentsOfDirectoryAtPath(sourcePath, error: nil)

And when I do this: println(imgArray) it always prints nil.

Why?? And what should I do?

  • println `sourcePath` and verify the file exists there with the finder if in the simulator or Xcode on a device. Simply it does not exist where you think it is or with the name you expect or at all. – zaph Nov 02 '14 at 15:30
  • 4
    What I really do not understand: If the function does not return what you expect, and the function has an `error` parameter, why don't you *use* that parameter and print the error in the case of a failure? – Martin R Nov 02 '14 at 15:33
  • Have a look at your "copy resources" build phase and see if the images really are being added into a "Pictures" subfolder within the bundle. – Abizern Nov 02 '14 at 17:35
  • Yes, I can see all the photos in "Copy Bundle Resources", I can't see the folder though. But what does this mean? And yes, I'm sure the files exists, I can see the folder inside my project folder in Finder. – Rodrigo Pelissier Nov 02 '14 at 18:40

1 Answers1

1

I finally made it work! :D

I added the folder directly in "Copy Bundle Resources" with the "+" button and now it properly returns an array containing the images. Thanks to Abizem who pointed out the copy bundle resources...