I have been going through many Swift and Objective-C posts over several hours to help me create a function that returns an array of the string filenames of a directory in xcassets. I then can use the array of filenames to create another function that displays a random image to my user.
var imageDirectory = [String]()
func getContentOfImageDirectory() -> [String] {
imageDirectory.removeAll(keepCapacity: false)
let fm = NSFileManager.defaultManager()
let sourcePath = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("DeviceImagesFolder")
var tempArray = fm.contentsOfDirectoryAtPath(sourcePath, error: nil)
if tempArray != nil {
self.imageDirectory = tempArray as! [String]
return imageDirectory
}
else {
println("tempArray was nil")
if fm.fileExistsAtPath(sourcePath){
println("There is a file")
return []
}
else {
println("There is not a file")
return []
}
}
}
However tempArray keeps returning nil and "There is not a file". And obviously my function returns the entire contains of "DeviceImagesFolder", rather than an array of the filenames.
If anyone could offer a solution to this problem it would be very much appreciated. Cheers.