I have an OS X app written in Swift and I am attempting to iterate through a directory and save each image into an array of NSImages so that I can display them in an NSImageView one at a time.
My code at the moment is below:
func LoadImages() {
let fileManager = NSFileManager.defaultManager()
let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(strPhotoDirectory)!
while let element = enumerator.nextObject() as? String {
if element.hasSuffix("jpg") || element.hasSuffix("jpeg") {
self.images.append(NSImage(contentsOfFile: element)!)
}
}
self.imageCurrent.image = self.images[0] // Sets my imageView to the first image
}
where strPhotoDirectory
is equal to a file path.
At the moment, the debugger crashes on the last line there (setting the imageView to the first image) because the array is out of range, which I take it to mean that the images were never saved in the first place, so the array is empty.
I've tried po
ing the debugger console to find some of the values of my variables as it runs and have found that the element.hasSuffix("XXX")
returns false for jpg
, jpeg
, JPG
, JPEG
. The image I am trying to save has the file name VH-YIZ VOZ B738 - YSSY - 26-07-2015 - 1 - Rotating on 34R
and the extension .JPEG
.
What am I doing wrong?
Thanks in advance.
Edit When I run the code, it reaches the line specified above and outputs this:
strPhotoDirectory = /Users/Matt/Downloads/Test/
Total Path = /Users/Matt/Downloads/Test/VH-YIZ VOZ B738 - YSSY - 10-04-2015 - 1 - On short final for 25.JPG
then:
fatal error: unexpectedly found nil while unwrapping an Optional value