I am working on a TVOS 10 project with Swift 3.0 and I am trying to access the files in the Assets folder from the controller.
I have this array:
var posters: [String] = ["image1", "image2", "image3","image4", "image5"]
But I want to populate this array programmatically. So if i change the files in the assets, the content of the array should change.
Because later I do this:
//currentImage is an ImageView in storyboard
currentImage.image = UIImage(named: posters[currentPoster])
This is the hierarchy of my files.
This is what I have tried but it does not give me what I want.
if let resourcePath = Bundle.main().resourcePath {
print(resourcePath)
do {
let temp = try FileManager.default().contentsOfDirectory(atPath: resourcePath)
print(temp)
let assetsPath = resourcePath + "/" + temp[6]
let temp2 = try FileManager.default().contentsOfDirectory(atPath: assetsPath)
print (temp2)
}
catch let error as NSError {
print(error)
}
}
Output:
/var/containers/Bundle/Application/9AECCDB0-DD5F-4377-9237-6E7DA1E14A39/PosterAppTV.app
["Assets.car", "Base.lproj", "Frameworks", "Info.plist", "META-INF", "PkgInfo", "PosterAppTV", "_CodeSignature", "embedded.mobileprovision", "libswiftRemoteMirror.dylib"]
Error Domain=NSCocoaErrorDomain Code=256 "The file “PosterAppTV” couldn’t be opened." UserInfo={NSFilePath=/var/containers/Bundle/Application/9AECCDB0-DD5F-4377-9237-6E7DA1E14A39/PosterAppTV.app/PosterAppTV, NSUserStringVariant=( Folder ), NSUnderlyingError=0x1740524e0 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}
Any help how to get a list of names of the files in the Assets/Posters folder? Also, if this may not be possible, are there other ways to store some pictures into a folder and then access the names of those files programmatically?