1

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 ?

JameS
  • 231
  • 1
  • 2
  • 11
  • Check your number of rows in section. May be you are trying to set no of rows above than data exist in your url. – Amit Kumar Oct 21 '17 at 13:45
  • @Amit is this the one you've mentioned ? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return fileURLs.count } – JameS Oct 21 '17 at 14:03
  • Yes, this is the method... Run your code again with placing breakpoint on cellforRowAt_indexpath. And check on which iteration your code is crashing. – Amit Kumar Oct 21 '17 at 14:13
  • @Amit let objectIdentifier = "ObjectsTableViewCell" – JameS Oct 21 '17 at 14:16
  • I think... This line in cellforrowatindexpath let currentFileParts = extractAndBreakFilenameInComponents(fileURL: fileURLs[indexPath.row]) is the cause of error. Because it may be chance that the data is not exist at position in fileUrl where you're accessing it. – Amit Kumar Oct 21 '17 at 14:17
  • yes, that was the first thing came into my mind because there is no initial file in the array – JameS Oct 21 '17 at 14:21

1 Answers1

2

Pretty explicit error. You are trying to access to an index out of the range of your array fileUrls of NSURL. With your code, I see one possible issue causing this kind of error.

As you know, you initialize an empty array at the beginning of your code. Then, in your private func prepareFileURLS() you fill your array by appending your NSURLS with this statement fileURLs.append(csvFile as NSURL). The question is did you really fulfill your array ? Did your code pass your if statement if FileManager.default.fileExists(atPath: csvFile.path)? I encourage you to print your fileURLs array to see what it contains and his length.

Then your isssue is going to propagate in your private func extractAndBreakFilenameInComponents(fileURL: NSURL) -> (fileName: String, fileExtension: String) with this statement return (filenameParts![0], filenameParts![1]). As your initial array fileURLs contains nothing you unwrap with your exclamation mark filenameParts which is not set and there is no value (=nil) causing your fatal error.

Arrabidas92
  • 1,113
  • 1
  • 9
  • 20
  • thanks!! this is exactly what I looking for. Yes, the array is still empty until the user creates the file in the app afterward. This might sound silly, but how can I return file path with optional value since there wouldn't be any file in the beginning. @Arrabidas92 – JameS Oct 21 '17 at 15:12
  • Make it optional with a ? in front of – Arrabidas92 Oct 21 '17 at 15:14