I've been trying to load the file in document directory to show on the uitableview and read them using quickview
var fileURLs: [NSURL] = []
every time I run the apps it error on this line as "index out of range" on fileURLs[indexPath.row]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if QLPreviewController.canPreview(fileURLs[indexPath.row]) {
quickLookController.currentPreviewItemIndex = indexPath.row
navigationController?.pushViewController(quickLookController, animated: true)
}
}
the fileURLs array is being appended by the method below
private func prepareFileURLS() {
let fileManager = FileManager.default
let path = fileManager.urls(for: .documentDirectory, in: .userDomainMask).last! as NSURL
let fileName = String(describing: path.lastPathComponent)
let pathString = String(describing: path.absoluteURL)
fileNames.append(fileName)
if FileManager.default.fileExists(atPath: pathString) {
fileURLs.append(path as NSURL)
print(fileURLs)
}
}
and the result every time is an empty array when I check it, and return only a document path not the file path itself
[]
Optional(file:///Users/N/Library/Developer/CoreSimulator/Devices/EC72CD97-87F4-478E-967C-6BA135B2EC6C/data/Containers/Data/Application/0778931B-77DD-4CE8-92BF-E9A8002F901D/Documents/)
when I manually checked the folder their is a file in there. Anyone knows what is wrong with the code above ?
added ** the file on the table show perfectly fine since it load name and other data from a model file except it can't be view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//tableView.reloadData()
return csvFiles.count
}
here is data model
class CSVData {
// MARK: Properties
var name: String
var descriptions: String
var photo: UIImage?
var url: NSURL
// MARK: Init
init?(name: String, descriptions: String?, photo: UIImage?, url: NSURL){
// init should fail if there is no name
if name.isEmpty {
return nil
}
self.name = name
self.descriptions = descriptions ?? ""
self.photo = photo
self.url = url
}
}
fileManager.urls returns array of directories on domain, not array of files in that directory. To get file paths you should use contentsOfDirectory of FileManager. See https://stackoverflow.com/a/27722526/6835150