一尘不染

在Swift中解析CSV文件

swift

应用启动时,我需要将数据预加载到tableView中。所以我通过解析.csv文件来使用核心数据。为此,我正在关注本教程。这是我的parseCSV函数

func parseCSV (contentsOfURL: NSURL, encoding: NSStringEncoding, error: NSErrorPointer) -> [(stationName:String, stationType:String, stationLineType: String, stationLatitude: String, stationLongitude: String)]? {
    // Load the CSV file and parse it
    let delimiter = ","
    var stations:[(stationName:String, stationType:String, stationLineType: String, stationLatitude: String, stationLongitude: String)]?

    let content = String(contentsOfURL: contentsOfURL, encoding: encoding, error: error)
    stations = []
    let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]

    for line in lines {
        var values:[String] = []
        if line != "" {
            // For a line with double quotes
            // we use NSScanner to perform the parsing
            if line.rangeOfString("\"") != nil {
                var textToScan:String = line
                var value:NSString?
                var textScanner:NSScanner = NSScanner(string: textToScan)
                while textScanner.string != "" {

                    if (textScanner.string as NSString).substringToIndex(1) == "\"" {
                        textScanner.scanLocation += 1
                        textScanner.scanUpToString("\"", intoString: &value)
                        textScanner.scanLocation += 1
                    } else {
                        textScanner.scanUpToString(delimiter, intoString: &value)
                    }

                    // Store the value into the values array
                    values.append(value as! String)

                    // Retrieve the unscanned remainder of the string
                    if textScanner.scanLocation < textScanner.string.characters.count {
                        textToScan = (textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1)
                    } else {
                        textToScan = ""
                    }
                    textScanner = NSScanner(string: textToScan)
                }

                // For a line without double quotes, we can simply separate the string
                // by using the delimiter (e.g. comma)
            } else  {
                values = line.componentsSeparatedByString(delimiter)
            }

            // Put the values into the tuple and add it to the items array
            let station = (stationName: values[0], stationType: values[1], stationLineType: values[2], stationLatitude: values[3], stationLongitude: values[4])
            stations?.append(station)
        }
    }


    return stations
}

这是我的示例.csv文件

Rithala,Underground,Yellow Line,28.7209,77.1070

但我在这条线上出现错误

let station = (stationName: values[0], stationType: values[1], stationLineType: values[2], stationLatitude: values[3], stationLongitude: values[4])
            stations?.append(station)

致命错误:数组索引超出范围

我究竟做错了什么 ?请帮我。


阅读 658

收藏
2020-07-07

共1个答案

一尘不染

您正在尝试解析文件路径而不是文件内容。

如果您更换

let content = String(contentsOfURL: contentsOfURL, encoding: encoding, error: error)

与:

if let data = NSData(contentsOfURL: contentsOfURL) {
  if let content = NSString(data: data, encoding: NSUTF8StringEncoding) {
    //existing code
  }
}

那么代码将适用于您的示例文件。

2020-07-07