1

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
}

}

JameS
  • 231
  • 1
  • 2
  • 11
  • You need to show your `numberOfRowsInSecrion` function since it seems that you have told the tableview that there are more rows than there are elements in your array. – Paulw11 Nov 05 '17 at 20:05
  • @Paulw11 I just added more information. the file shows perfectly on the table except it can't be view. what I guess is due to it can't access the url of it. The data model also include its url but I don't know exactly how to use it though – JameS Nov 05 '17 at 20:15
  • 1
    `absoluteURL` doesn't return a path. To get the url path you should get its `path` property. `fileExists(atPath:)` will always fail if you pass the url absoluteString. `FileManager.default.fileExists(atPath: url.path)` . And don't use `String(describing:)` – Leo Dabus Nov 05 '17 at 21:56
  • Btw you should be using URL not NSURL since Swift 3 – Leo Dabus Nov 05 '17 at 21:57

1 Answers1

1

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