表格视图错误致命错误:加载文件时索引超出范围 [英] Table view error Fatal error: Index out of range when loading file

查看:212
本文介绍了表格视图错误致命错误:加载文件时索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将URL路径加载到我的tableview中,为quicklook框架做准备

I'm trying to load the url path into my tableview preparing for quicklook framework

使用下面的代码从首次创建时为空的.document中获取网址

using the code below to fetch the url from .document where it is empty when it was first created

var fileURLs = [NSURL]()

然后

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 ?

推荐答案

相当明确的错误.您正在尝试访问NSURL数组fileUrls范围之外的索引. 通过您的代码,我看到一个可能的问题导致了这种错误.

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.

您知道,您在代码的开头初始化了一个空数组.然后,在private func prepareFileURLS()中,通过在NSURLS后面附加此语句fileURLs.append(csvFile as NSURL)来填充数组.问题是您真的完成了阵列吗?我的代码是否通过了if语句if FileManager.default.fileExists(atPath: csvFile.path)??我建议您打印fileURLs数组以查看其包含的内容及其长度.

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.

然后,您的问题将通过此语句return (filenameParts![0], filenameParts![1])在您的private func extractAndBreakFilenameInComponents(fileURL: NSURL) -> (fileName: String, fileExtension: String)中传播.由于您的初始数组fileURLs不包含任何内容,因此您不会使用未设置的感叹号filenameNameParts进行解包,并且没有导致致命错误的值(= nil).

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.

这篇关于表格视图错误致命错误:加载文件时索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆