从API快速解析CSV文件不与定界符分开 [英] swift parsing CSV file from API does not separate with the delimiter

查看:131
本文介绍了从API快速解析CSV文件不与定界符分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据传递到tableView的单元格中.网络通讯之所以有效,是因为项目列表出现在第一个单元格中.

I'm trying to pass the data into the cells of a tableView. The networking communication works because The list of items appear in the first cell.

例如,列表如下:sensor1,sensor2,sensor3,.... 但它应该像:

For example the list come like: sensor1, sensor2, sensor3,.... but it should be like :

sensor1

sensor2

...

这就是我解析CSV文件

struct ParseCVS {

func parseURL (contentsOfURL: NSURL, encoding: NSStringEncoding) -> ([String])?{
    let rowDelimiter = ","
    var nameOfSensors:[String]?

    do {
        let content = try String(contentsOfURL: contentsOfURL, encoding: encoding)
        print(content)

        nameOfSensors = []

        let columns:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]


        for column in columns {
            let values = column.componentsSeparatedByString(rowDelimiter)
            if let nameOfSensor = values.first {
                nameOfSensors?.append(nameOfSensor)
            }    
        }

    }
    catch {
        print(error)
    }

    return nameOfSensors

  }

}

这是我的 TableViewController

class TableViewController: UITableViewController {

// Array which will store my Data
var nameOfSensorsList = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    guard let wetterURL = NSURL(string: "http://wetter.htw-berlin.de/phpFunctions/holeAktuelleMesswerte.php?mode=csv&data=1")
            else {
                return
              }

    let parseCSV = ParseCVS()
    nameOfSensorsList = parseCSV.parseURL(wetterURL, encoding: NSUTF8StringEncoding)!

    tableView.estimatedRowHeight = 100.0

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return nameOfSensorsList.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuTableViewCell

    cell.nameLabel?.text = nameOfSensorsList[indexPath.row] 

    return cell
  }


}

如果有人有任何想法,我将非常感谢.

if someone have any ideas I would really appreciate it.

推荐答案

您忘了遍历值"数组. 尝试这样的事情:

You've forgotten to iterate through an array of "values". Try something like this:

for column in columns {
    let values = column.componentsSeparatedByString(rowDelimiter)

    print(values.count)
    for value in values {
        nameOfSensors?.append(value)
    }
}

这篇关于从API快速解析CSV文件不与定界符分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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