I'm trying to load the url path into my tableview preparing for quicklook framework
using the code below to fetch the url from .document where it is empty when it was first created
var fileURLs = [NSURL]()
then
private func prepareFileURLS() {
let csvFile = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
if FileManager.default.fileExists(atPath: csvFile.path) {
fileURLs.append(csvFile as NSURL)
print(fileURLs)
}
}
then using the code below to give the label a name
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let objectIdentifier = "ObjectsTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: objectIdentifier, for: indexPath) as? ObjectsTableViewCell else {
fatalError("The dequeued cell is not an instance of ObjectsTableViewCell")
}
//quickview
// Fetches the appropriate object for the data source layout
let currentFileParts = extractAndBreakFilenameInComponents(fileURL: fileURLs[indexPath.row])
cell.nameLabel.text = currentFileParts.fileName
//cell.photoImageView.image = cur.photo
//quickview
cell.descriptionLabel.text = getFileTypeFromFileExtension(fileExtension: currentFileParts.fileExtension)
return cell
}
and using the method below to break down path into string where it cause the error
private func extractAndBreakFilenameInComponents(fileURL: NSURL) -> (fileName: String, fileExtension: String) {
// Break the NSURL path into its components and create a new array with those components.
let fileURLParts = fileURL.path!.components(separatedBy: "/")
// Get the file name from the last position of the array above.
let fileName = fileURLParts.last
// Break the file name into its components based on the period symbol (".").
let filenameParts = fileName?.components(separatedBy: ".")
// Return a tuple.
return (filenameParts![0], filenameParts![1]) --> Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
}
fatal error: Index out of range what did I do wrong ?